如何让 x 轴只显示我想绘制的数据?而不是包括没有情节的标签?
How do i get the x axis to only show my data i want to plot? Instead of it including labels with no plots?
我正在绘制日期与整数。但是,由于日期范围相隔 100 天,matplotlib 会自动包含 100 天之间的每个日期,即使只有 20 天左右的数据要绘制。所以我的问题是,如何让 x 轴只生成有数据要绘制的日期?此外,目前日期以水平方式被压缩,我如何以垂直方式获得它们以便更多可以容纳?
这是我目前的图表图片:
这是我的代码:
import datetime as dt
import matplotlib.dates as mdates
array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
array2 = [1,4,5,6,9]
x = [dt.datetime.strptime(a,'%Y-%m-%d').date() for a in array1]
plt.plot_date((x), (array2), 'ro')
plt.show()
您可以旋转 xtick 标签:
plt.xticks(rotation=45)
并使用以下方法沿 x 轴 均匀 绘制日期:
x = np.arange(n)
ax.plot(x, array2, 'ro')
然后重新标记 xticks:
locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)
但是请注意,手动设置位置和标签的副作用是
当您使用 GUI 平移或缩放工具时,刻度将不再像通常那样自动适应比例变化。
import time
import datetime as dt
import numpy as np
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
n, m = 20, 30
now = dt.date.today()
dates = [now+dt.timedelta(days=i) for i in range(n//2)+range(m,m+n//2)]
x = np.arange(n)
array2 = np.random.random(n)
fig, ax = plt.subplots()
ax.plot(x, array2, 'ro')
locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)
plt.xticks(rotation=45)
plt.show()
产量
好的,首先,您可以使用 fig.autofmt_xdate()
函数自动处理 xtick 标签 - 这是最简单的方法。
所以你会有这样的东西:
import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint
array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
array2 = [1,4,5,6,9]
dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,32)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot_date(dates, freqs, "ro")
fig.autofmt_xdate()
pyplot.show()
仍然有你不想要的大差距,但刻度标签更好。
接下来,要处理拆分,有几个选项,但它们还不在主要的 matplotlib 中(据我所知)。这是通过实际绘制两个图,删除中间的刺,并使用 sharey
选项来完成的:
import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint
from matplotlib.ticker import MaxNLocator
dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,10)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]
fig = pyplot.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2, sharey=ax1)
ax1.plot_date(dates, freqs, "ro")
ax2.plot_date(dates, freqs, "ro")
#set the upper and lower bounds for the two adjacent plots
ax1.set_xlim(xmax=dt.datetime.strptime("2014-01-11", '%Y-%m-%d').date())
ax2.set_xlim(xmin=dt.datetime.strptime("2014-05-01", '%Y-%m-%d').date())
for ax in [ax1, ax2]:
_ = ax.get_xticklabels() #For some reason, if i don't do this, then it only prints years for the tick labels. :/
ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=6))
#Turn off the spines in the middle
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax1.yaxis.tick_left()
ax2.get_yaxis().set_visible(False)
pyplot.subplots_adjust(wspace=0.1)
fig.autofmt_xdate()
pyplot.show()
还有一些额外的东西需要清理,但我想你明白了。
我正在绘制日期与整数。但是,由于日期范围相隔 100 天,matplotlib 会自动包含 100 天之间的每个日期,即使只有 20 天左右的数据要绘制。所以我的问题是,如何让 x 轴只生成有数据要绘制的日期?此外,目前日期以水平方式被压缩,我如何以垂直方式获得它们以便更多可以容纳?
这是我目前的图表图片:
这是我的代码:
import datetime as dt
import matplotlib.dates as mdates
array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
array2 = [1,4,5,6,9]
x = [dt.datetime.strptime(a,'%Y-%m-%d').date() for a in array1]
plt.plot_date((x), (array2), 'ro')
plt.show()
您可以旋转 xtick 标签:
plt.xticks(rotation=45)
并使用以下方法沿 x 轴 均匀 绘制日期:
x = np.arange(n)
ax.plot(x, array2, 'ro')
然后重新标记 xticks:
locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)
但是请注意,手动设置位置和标签的副作用是 当您使用 GUI 平移或缩放工具时,刻度将不再像通常那样自动适应比例变化。
import time
import datetime as dt
import numpy as np
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
n, m = 20, 30
now = dt.date.today()
dates = [now+dt.timedelta(days=i) for i in range(n//2)+range(m,m+n//2)]
x = np.arange(n)
array2 = np.random.random(n)
fig, ax = plt.subplots()
ax.plot(x, array2, 'ro')
locs = x
labels = [d.strftime('%b %d') for d in dates]
plt.xticks(locs, labels)
plt.xticks(rotation=45)
plt.show()
产量
好的,首先,您可以使用 fig.autofmt_xdate()
函数自动处理 xtick 标签 - 这是最简单的方法。
所以你会有这样的东西:
import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint
array1 = ['2014-10-28', '2014-11-17', '2014-09-29', '2014-10-17', '2014-10-22']
array2 = [1,4,5,6,9]
dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,32)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot_date(dates, freqs, "ro")
fig.autofmt_xdate()
pyplot.show()
仍然有你不想要的大差距,但刻度标签更好。
接下来,要处理拆分,有几个选项,但它们还不在主要的 matplotlib 中(据我所知)。这是通过实际绘制两个图,删除中间的刺,并使用 sharey
选项来完成的:
import datetime as dt
import matplotlib.dates as mdates
from matplotlib import pyplot
from random import randint
from matplotlib.ticker import MaxNLocator
dates = ["2014-{month:0>2d}-{day:0>2d}".format(month=m, day=d) for m in [1,5] for d in range(1,10)]
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
freqs = [randint(0,4) for _ in dates]
fig = pyplot.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2, sharey=ax1)
ax1.plot_date(dates, freqs, "ro")
ax2.plot_date(dates, freqs, "ro")
#set the upper and lower bounds for the two adjacent plots
ax1.set_xlim(xmax=dt.datetime.strptime("2014-01-11", '%Y-%m-%d').date())
ax2.set_xlim(xmin=dt.datetime.strptime("2014-05-01", '%Y-%m-%d').date())
for ax in [ax1, ax2]:
_ = ax.get_xticklabels() #For some reason, if i don't do this, then it only prints years for the tick labels. :/
ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=6))
#Turn off the spines in the middle
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax1.yaxis.tick_left()
ax2.get_yaxis().set_visible(False)
pyplot.subplots_adjust(wspace=0.1)
fig.autofmt_xdate()
pyplot.show()
还有一些额外的东西需要清理,但我想你明白了。