我正在尝试绘制股票图表。 我的DataFrame结构如下:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 503 entries, 0 to 506
Data columns (total 6 columns):
Date 503 non-null object
Open 503 non-null float64
High 503 non-null float64
Low 503 non-null float64
Close 503 non-null float64
Volume 503 non-null float64
dtypes: float64(5), object(1)
memory usage: 27.5+ KB
当我尝试使用“关闭”列绘制数据时,我在x轴上得到了数字,但是我想使用3个月的滴答来绘制带有日期的数据。我该如何实现?
此刻的代码如下:
dax_data["Close"].plot(label = "Dax Close", figsize = (16,8), title = "Dax Prices")
plt.legend();
答案 0 :(得分:0)
这是因为默认情况下,“ DataFrame.plot()”使用数据帧索引作为x值。 您应该这样做:
dax_data.plot(x = 'Date', y = 'Close', label = "Dax Close", figsize = (16,8), title = "Dax Prices")
并有3个月的滴答声:
import matplotlib.dates as mdates
### Your code to get dax_data
ax = dax_data.plot(x = 'Date', y = 'Close', label = "Dax Close", figsize = (16,8), title = "Dax Prices")
ax.xaxis.set_major_locator(mdates.MonthLocator(interval = 3)) # to display ticks every 3 months
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%Y")) # to set how dates are displayed
plt.legend()