【问题标题】:How can I increase the frequency of xticks/ labels for dates on a bar plot?如何增加条形图上日期的 xticks/标签的频率?
【发布时间】:2014-09-24 01:06:15
【问题描述】:

我正在尝试增加条形图上日期的 xtick 和标签频率。它目前每 2 个月放置一次标签,我想将其增加到每个月。

我目前的代码是:

fig = plt.figure()
ax = plt.subplot(1,1,1)

# defining the spacing around the plots
plt.subplots_adjust(left = 0.125, bottom = 0.1, right = 0.9, top = 0.9, wspace = 0.2, hspace = 0.35)

handles = []        # for bar plot  

bar1 = ax.bar(dates, clim[loc,:], label = 'climatology (1981 - 2013)', color='darkgray', width=width_arr[:], linewidth=0, alpha=0.5)

handles.append(bar1)

bar2 = ax.bar(dates, chirps[i,loc,:], label = str(year1), color = 'blue', width=width_arr[:], linewidth=0, alpha=0.45)
handles.append(bar2)


# setting the plot tick labels   
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(10)

for tick in ax.yaxis.get_major_ticks():
    tick.label.set_fontsize(10)

ax.set_ylabel("Precipitation", fontsize=10)

# plotting the legend within the plot space
plt.legend( loc = 'upper left', numpoints=1, ncol = 3, prop={'size':10})
plt.ylim(0, 110)

# rotating the tick marks to make best use of the x-axis space
plt.xticks(rotation = '25') 

【问题讨论】:

  • 您提供的代码没有太大帮助,因为我们不知道datesclim 是什么样的。尝试添加一个显示您所拥有的最小工作示例,不要包含不相关的 plt.subplots_adjust 之类的内容。这将提高答案的质量/数量。

标签: python date matplotlib plot bar-chart


【解决方案1】:

您可以在xticks 命令中指定 xtick 位置。因此,在您的情况下,这应该可以解决问题:而不是

plt.xticks(rotation = '25')  

使用

plt.xticks(dates,rotation = '25')

这是一个minimal working example,它说明了您的问题和解决方法。

import numpy as np
import matplotlib.pyplot as plt

# generate random data
x = [1.0, 2.0, 3.0, 4.0, 5.0]
y = np.random.rand(2,5)

# setup the plot
fig = plt.figure()
ax = plt.subplot(1,1,1)

# plot the data, only the bar command is important:
bar1 = ax.bar(x, y[0,:], color='darkgray', width=0.5, linewidth=0, alpha=0.5)
bar2 = ax.bar(x, y[1,:], color = 'blue', width=0.5, linewidth=0, alpha=0.45)

# defining the xticks
plt.xticks(rotation = '25')
#plt.xticks(x,rotation = '25')

plt.show()

这样会生成下图:

在此图中,我们看到的标签比我们希望的要多得多 - 这与您看到的相反,但是答案是相同的。
现在,如果您更改plt.xticks 命令(在上面的示例中,您可以注释一行并取消注释另一行),该图将变为:

y 数据已更改的事实(由于np.random 的调用是无关紧要的。但是,您可以看到,现在我们在x 中给出的位置只有 x-ticks。

如果你想跳过某些标签,我建议使用numpy's arange 函数。 arangelinspace 不同,它允许固定增量(第三个输入参数)。您可以将plt.xticks(x,rotation='25') 行替换为以下代码示例:

# defining the xticks
names = ['one', 'two', 'three', 'four', 'five']

x_tick_location = np.arange(np.min(x),np.max(x)+0.001,2.0)

x_tick_labels = [names[x.index(xi)] for xi in x_tick_location]

plt.xticks(x_tick_location,x_tick_labels,rotation = '25')

这将为您提供以下情节:

有几点值得指出:

  • 我介绍了一个列表names,其中包含用作刻度标签的字符串。我强烈建议不要设置这样的标签,因为它会使您的刻度标签与数据分离。在这里,它有助于说明xticks 的工作原理。
  • 如上所述,刻度位置使用np.arange 定义。在此示例中,在 xminmax 条目之间,我们增加了 2。向max 值添加少量以确保可以考虑它可能会有所帮助(这与浮点运算有关)。
  • 此命令[names[x.index(xi)] for xi in x_tick_location] 通过从x_tick_location 中获取每个元素,在原始x 中找到其索引并使用该索引来选择names 中的相应元素来创建一个列表。
  • 最后,将刻度位置和刻度标签都传递给xticks 命令。

【讨论】:

  • 感谢您的回答。这似乎非常接近我正在寻找的答案。理想情况下,我想绘制日期;一年有 72 个条形图(每个月有 6 个条形图),我想根据每个月的第一个条形图绘制月份标签,例如使用 x = ['Jan'、'Feb'、'Mar' 等]。是否可以将 xticks 设置为每 6 个柱标记一次?
  • 太完美了!非常感谢您的帮助并花时间写出如此详细的答案。我会投票支持你的答案,但目前没有足够的投票权。
猜你喜欢
  • 2016-06-01
  • 2020-10-28
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
相关资源
最近更新 更多