如何让这个情节的x轴看起来更好?
在绘制数据框中的所有值时,有没有办法在x轴上只显示3个值?
我尝试使用plt.xticks(rotation=70)
,但它仍然会削减我的数字..
如您所见,它以MMM DD YYYY
格式显示日期。
我想要
MMM DD
或DD MMM
之类的东西。甚至写在2 行DD MM \n YYYY
。我怎样才能做到这一点?
这是用于生成图表的代码:
plt.plot(df_21d["Timestamp"], df_21d["Close"], label="Price", color="b")
plt.title("BTC - 21 Dias")
plt.xlabel("Data")
plt.ylabel("Preco (em USD)")
plt.grid(True)
plt.show()
以下是我的数据:
df_21d [ “时间戳”]
667 2016-05-27 08:00:00
668 2016-05-27 08:30:00
669 2016-05-27 09:00:00
670 2016-05-27 09:30:00
671 2016-05-27 10:00:00
Name: Timestamp, dtype: datetime64[ns]
df_21d [ “关闭”]
667 480.00
668 471.36
669 477.35
670 476.55
671 479.92
Name: Close, dtype: float64
我不知道是否必须格式化df_21d.Timestamp
或者它是否是matplotlib配置。也许格式化时间戳并使用plt.xticks()
将其作为向量传递...但我不确定这是怎么做的......
或者甚至可能只插入x轴的第一个,最后一个和中间值,例如:
plt.xaxis_labels = (df_21d["Timestamp"][1],
df_21d["Timestamp"][len(df_21d["Timestamp"])/2],
df_21d["Timestamp"][len(df_21d["Timestamp"])])
答案 0 :(得分:0)
这个答案可能对您有所帮助: Not write out all dates on an axis, Matplotlib
基本上,您可以通过设置格式来设置日期格式:
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %m'))