【问题标题】:Plotting Area plots with x axis formatting使用 x 轴格式绘制区域图
【发布时间】:2020-07-10 20:53:43
【问题描述】:

我有以下代码:

population = pd.DataFrame(data = population)
fig, ax = plt.subplots(figsize = (10, 5))
population.columns = ['pop']
# ax.area(population['pop'], kind ='area')
# ax = population.plot.area()


ax.fill_between: ax.fill_between(population.index, population['pop'], alpha=0.3, label='Population')
ax.xaxis.set_major_locator(md.YearLocator())
ax.xaxis.set_major_formatter(md.DateFormatter('%Y'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
ax.set_xlim([population.index[0], population.index[-1]])
ax.fill_between(population['pop'],0)

这使得下面的情节:

如何绘制面积图。如果我这样做:

ax.plot(population['pop'], kind ='area')

我收到以下错误:

AttributeError: 'Line2D' object has no property 'kind'

如果我这样做:

ax = population.plot.area()

它将整个格式保留在 x 轴上。我如何绘制区域? 编辑:

我得到以下情节

图例:

我添加了以下行:

ax.fill_between: ax.fill_between(population.index, population['pop'], alpha=0.3, label='Population')

如你所见,没有传说

【问题讨论】:

  • 在您的第一个解决方案中尝试ax.fill_between(population.index, population['pop'],0)

标签: python pandas matplotlib


【解决方案1】:

ax.fill_between()代替ax.plot()

plt.fill_between(your_x_list, your_y_list, label=your_label)

【讨论】:

  • 你能发布结果吗?
  • @Slartibartfast 您需要致电ax.legend() 才能看到图例
  • @Slartibartfast 你可以看到stackoverflow.com/q/59346731/13552470
  • 您需要在对ax.plot和/或ax.fill_between的调用中设置标签:ax.fill_between(population.index, population['pop'], alpha=0.3, label='Population')
  • @Slartibartfast 你需要调用plt.legend(),不带参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 2017-11-02
  • 2021-02-22
  • 2018-03-12
  • 2019-10-15
相关资源
最近更新 更多