【问题标题】:Pandas & Matplotlib: personalize the date format in a line chartPandas 和 Matplotlib:个性化折线图中的日期格式
【发布时间】:2020-04-30 23:06:52
【问题描述】:

我想让 x 轴上的日期看起来更漂亮,目前甚至无法读取日期。最好的方法是什么。 下面是代码,也是实际的图形图片

import matplotlib.pyplot as plt
import pandas as pd
import pandas as pd

df = dataset
# gca stands for 'get current axis'
ax = plt.gca()
y1 = df['Predicted_Lower']
y2 = df['Predicted_Upper']
x = df['Date']
ax.fill_between(x,y1, y2, facecolor="#CC6666", alpha=0.7)
df.plot(kind='line',x='Date',y='Predicted_Lower',color='white',ax=ax)
df.plot(kind='line',x='Date',y='Predicted_Upper',color='white', ax=ax)
df.plot(kind='line',x='Date',y='Predicted', color='yellow', ax=ax)
df.plot(kind='line',x='Date',y='Actuals', color='green', ax=ax)
plt.xticks(rotation=45)

plt.show()

【问题讨论】:

标签: python-3.x pandas matplotlib data-visualization predictive


【解决方案1】:

您可以修改标签的数量,通过使用matplotlib.pyplot.xticks 设置locslabels 参数,例如获取当前的locslabels 并且只绘制其中的三分之一:

# ...
df.plot(kind='line',x='Date',y='Actuals', color='green', ax=ax)
locs, labels = plt.xticks()
plt.xticks(locs[::3], labels[::3], rotation=45)
plt.show()

【讨论】:

  • 尽量少用plt.xticks(locs[::5], labels[::5], rotation=45)这样的项目,没有数据就很难判断。
  • 所有日期显示为 00-00-00
  • 好吧,根据你的照片,所有日期都是00-00-00,你能不能把数据框的前30行贴出来:df.head(30).to_json()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 2019-06-25
  • 2019-01-19
  • 2014-05-30
  • 1970-01-01
  • 2019-04-28
相关资源
最近更新 更多