[英]Stacked area plot and dates in matplotlib
Here is my code: 这是我的代码:
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
dates = ['2014-12-15','2015-11-07','2016-01-10']
x = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.stackplot(x,[1,2,3],[7,8,3])
plt.gcf().autofmt_xdate()
plt.show()
It produces the following plot: 它产生以下图:
How do I fix it to have only 3 dates? 如何解决只有3个日期的问题? I followed the following example: SO example
我遵循以下示例: SO示例
You just need to set xticks
with the following command: 您只需要使用以下命令设置
xticks
:
plt.xticks(x)
Just add this line before you display the figure. 在显示该图之前,只需添加此行。
DayLocator
displays every single day by default. 默认情况下,
DayLocator
显示每一天。 In your case, you have too many days in between to display. 就您而言,您之间的间隔时间太多了。 You can just remove line 7 which sets the
Daylocator
and replace it with the above line which sets the ticks to be your dates. 您只需删除设置
Daylocator
第7行,然后将其替换为将行号设置为日期的上述行即可。 This is the result: 结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.