【问题标题】:What would be the best way to plot a dataset with months as x ticks将月份绘制为 x 刻度的数据集的最佳方法是什么
【发布时间】:2021-03-26 05:06:02
【问题描述】:

我正在尝试绘制以下数据集,但是,由于某种原因,我无法正确获取刻度。似乎他们总是不在位。

y = 
[0.72727273,0.5,0.33333333,0.4375,0.4375,0.14285714,0.36363636,1.44444444,0.53333333,0.26666667,0.625,0.71428571,0.4,1.21428571,0.22222222,0.09090909,0.35714286,0.07142857,0.4,1.46666667,0,1.44444444,0.42857143,0.13333333,0.5,0.25,0,0.44444444,0.33333333,0.46153846,0,0.57142857,0.8,0.35714286,0.16666667,0.45454545,0,0.95,0,0,0,0.15384615,0.5,1,3.11111111,3,2.7,0.8,1.66666667,0.69230769,0,1.66666667,1,3,6.5,3.75,0,6,0,0]
month_and_year_name =
['Jun 2014', 'Jul 2014', 'Aug 2014', 'Sep 2014', 'Oct 2014', 'Nov 2014', 'Dec 2014', 'Jan 2015', 'Feb 2015', 'Mar 2015', 'Apr 2015', 'May 2015', 'Jun 2015', 'Jul 2015', 'Aug 2015', 'Sep 2015', 'Oct 2015', 'Nov 2015', 'Dec 2015', 'Jan 2016', 'Feb 2016', 'Mar 2016', 'Apr 2016', 'May 2016', 'Jun 2016', 'Jul 2016', 'Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016', 'Dec 2016', 'Jan 2017', 'Mar 2017', 'Apr 2017', 'May 2017', 'Jun 2017', 'Jul 2017', 'Aug 2017', 'Sep 2017', 'Aug 2018', 'Sep 2018', 'Oct 2018', 'Nov 2018', 'Dec 2018', 'Feb 2019', 'Mar 2019', 'Apr 2019', 'May 2019', 'Jun 2019', 'Jul 2019', 'Aug 2019', 'Sep 2019', 'Oct 2019', 'Dec 2019', 'Jan 2020', 'Feb 2020', 'Mar 2020', 'Sep 2020', 'Oct 2020', 'Nov 2020']

我正在使用以下代码来进行绘图。

    fig, ax = plt.subplots(figsize = (10,4))  

    ax.bar(month_and_year_name,y, width =1 , align = 'center', linewidth = 0.5, edgecolor = 'yellow' )
    ax.tick_params(axis='x', rotation=45)

    plt.tight_layout()

    plt.savefig("files/sparks_in_all_logs.png", dpi = 300)

如您所见,不仅刻度完全错位,它们也超出了我保存的 png。

我在这里做错了什么?我应该怎么做才能纠正这个情节?而且,对于未来,我将在 y 和 month_year 这两个参数中添加越来越多的信息。考虑到这一点,有没有办法正确设置图形大小? (每个月都会有一对新的 y 和 month_year)

在前几个月我设法得到了正确的情节,但同样的代码没有我想要的效果。

【问题讨论】:

  • 使用pd.to_datetime(df['month_year'])将month_year转换为日期时间,然后使用month_year.dt.month获取月份编号
  • @JoeFerndz 但我也需要年份...

标签: python-3.x matplotlib


【解决方案1】:

首先将month_and_year_name 转换为正确的datetime 对象并使用这些日期进行绘图:

from datetime import datetime
x = [datetime.strptime(name, '%b %Y') for name in month_and_year_name]

import matplotlib.dates as mdates
fig, ax = plt.subplots(figsize=(15, 4))  
h = ax.bar(x, y, width=5, align='center', linewidth=0.5, edgecolor='yellow')

Increase the tick frequency 每个柱有 1 个刻度:

start, end = ax.get_xlim()
xticks = np.linspace(start, end, len(x))
ax.set_xticks(xticks)
ax.set_xticklabels(xticks)

Fix the tick/bar alignment:

xticks_pos = [0.65*bar.get_width() + bar.get_xy()[0] for bar in h]
plt.xticks(xticks_pos, x,  ha='center', rotation=90)

并用set_major_formatter()设置轴日期格式:

fmt = mdates.DateFormatter('%b %Y') # '%b %Y' is 'Mon YYYY'
ax.xaxis.set_major_formatter(fmt)

【讨论】:

  • 看起来很棒!但我需要每个垃圾箱都有它的年份和月份......
  • 好的,你可以increase the tick frequency manually。我也会在这里更新代码。
  • 好的,看看更新后的代码是不是你要的。每个 x 值有 1 个刻度,xtick positioning is fixed
  • 太完美了!我做了一些调整,我的问题比这更困难,但它工作得很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2010-10-23
  • 2010-09-05
相关资源
最近更新 更多