[英]How to create a grouped bar chart (by month and year) on the x-axis and values on the y-axis using matplotlib.pyplot?
[英]Python: year or month on x axis with pyplot, pandas and matplotlib?
有很多不同的解決方案(大多數都已過時)來解決matplotlib對象中添加軸的問題,但尚未解決任何問題。 我想在x軸上獲取日期標簽(月或年)。
微機
import pandas as pd
from matplotlib.pyplot import savefig
a=pd.read_csv("test.csv")
a.pivot(index='date', columns='group').plot()
savefig("plot3.png")
test.csv
date,group,myval
2018-07-01,test1,999
2018-07-01,test2,523
2018-07-02,test1,3305
2018-07-02,test2,1290
2018-07-03,test1,9399
2018-07-03,test2,1827
2018-07-04,test1,6982
2018-07-04,test2,2391
2018-07-05,test1,4100
2018-07-05,test2,2081
2018-07-06,test1,0268
2018-07-06,test2,4834
2018-07-07,test1,9571
2018-07-07,test2,6369
2018-07-08,test1,3943
2018-07-08,test2,6483
缺少時間注釋的地方。
注釋具有以下解決方案,經過測試的解決方案可以工作。
I.將日期列轉換為datetime dtype a['date'] = pd.to_datetime(a['date'])
並繪圖。
a=pd.read_csv("test.csv")
a['date'] = pd.to_datetime(a['date'])
a.pivot(index='date', columns='group').plot()
在哪里根據需要調整格式,例如
a['date'] = pd.to_datetime(a['date'], format='%Y-%m-%d')
二。 通過a = pd.read_csv("test.csv", parse_dates=[0])
將列日期轉換為日期時間
a=pd.read_csv("test.csv", parse_dates=[0])
a.pivot(index='date', columns='group').plot()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.