python даты matplotlib складываются вместе - PullRequest
0 голосов
/ 27 апреля 2020

Вот мой код:

# Graph for both infections and closures

# # plotting the points  
plt.plot(graph_date, graph_daily_infections, label = "Infections per day")
plt.plot(graph_date, graph_total_infections, label = "Infection overall")
plt.plot(graph_date, graph_daily_closure, label = "Closures per day")  
plt.plot(graph_date, graph_total_closure, label = "Closure overall") 
# # naming the x axis 
plt.xlabel('Date') 
# naming the y axis 
plt.ylabel('Number of Infections/Closure') 
# giving a title to my graph 
plt.title('Daily infections and closure overtime \n Infection Rate: {0} | Closure Threshold: {1}'.format(infectionRate,closeThreshold)) 
# show a legend on the plot
plt.legend()
# # changing the scale of the x ticks at the bottom

# # plt.locator_params(nbins=4)

# # set size of the graph
plt.rcParams["figure.figsize"] = (20,15)
# # function to show the plot

plt.show()

Проблема с этим кодом заключается в том, что даты отображаются вместе на оси х, когда они отображаются. См. Ниже: enter image description here

Есть ли способ показать только месяцы или показать только месяцы и годы? интервал, для которого на графике должны отображаться данные, составляет 4 месяца, поэтому идеальным будет показывать только месяцы / год и месяц. Спасибо!

1 Ответ

0 голосов
/ 27 апреля 2020

Попробуйте использовать autofmt_xdate() для автоматического форматирования оси X.

Согласно matplotlib.org , вам необходимо добавить это до plt.show():

fig, ax = plt.subplots()
ax.plot(date, r.close)

# rotate and align the tick labels so they look better
fig.autofmt_xdate()

А для месяцев и лет вы можете добавить:

ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...