如何在x轴上绘制日期

时间:2018-05-13 18:36:35

标签: python pandas matplotlib

所以我把这个df与第一列称为“周”:

0     2018-01-07
1     2018-01-14
2     2018-01-21
3     2018-01-28
4     2018-02-04
5     2018-02-11
6     2018-02-18
7     2018-02-25
8     2018-03-04
9     2018-03-11
10    2018-03-18
11    2018-03-25
12    2018-04-01
13    2018-04-08
14    2018-04-15
15    2018-04-22
16    2018-04-29
17    2018-05-06
Name: Week, dtype: object

其他三个列具有不同的名称和整数作为值。 我的想法是将这些日期绘制在X轴上,其他3列用整数绘制在Y. 我已经尝试了我发现的所有东西,但还没有任何工作......

我做了:

df.set_index('Week')
df.plot()
plt.show()

哪种方法效果很好,但X轴在范围(0,17)中浮动......

我也尝试过:

df['Week'] = pd.to_datetime(df['Week'])
df.set_index('Week')
df.plot()
plt.show()

但我收到了这个错误:

Traceback (most recent call last):
  File "C:\Users\mar\Desktop\Web Dev\PJ E\EZA.py", line 33, in <module>
    df.plot()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 2677, in __call__
    sort_columns=sort_columns, **kwds)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 1902, in plot_frame
    **kwds)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 1729, in _plot
    plot_obj.generate()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 258, in generate
    self._post_plot_logic_common(ax, self.data)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 397, in _post_plot_logic_common
    self._apply_axis_properties(ax.yaxis, fontsize=self.fontsize)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 470, in _apply_axis_properties
    labels = axis.get_majorticklabels() + axis.get_minorticklabels()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\axis.py", line 1188, in get_majorticklabels
    ticks = self.get_major_ticks()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\axis.py", line 1339, in get_major_ticks
    numticks = len(self.get_major_locator()())
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dates.py", line 1054, in __call__
    self.refresh()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dates.py", line 1074, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dates.py", line 832, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dates.py", line 441, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Users\mar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dates.py", line 256, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以在下面做这样的事情:

df['Week'] = pd.to_datetime(df['Week'])
df.set_index('Week', inplace=True)
df.plot()