简体   繁体   English

Python:使用pyplot,pandas和matplotlib在x轴上的年或月?

[英]Python: year or month on x axis with pyplot, pandas and matplotlib?

There are plethora of different solutions, mostly outdated, to address the addition of axis in matplotlib object, haven't got any of them working yet. 有很多不同的解决方案(大多数都已过时)来解决matplotlib对象中添加轴的问题,但尚未解决任何问题。 I want to get date labels (either Months or Years) on the x axis. 我想在x轴上获取日期标签(月或年)。

How to add the date annotations on the axis in Python with Pandas and Matplotlib? 如何使用Pandas和Matplotlib在Python中的轴上添加日期注释?

MWE 微机

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 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

在此处输入图片说明

where the time annotations are missing. 缺少时间注释的地方。

Comments have the following solutions, solutions tested to work. 注释具有以下解决方案,经过测试的解决方案可以工作。

I. Convert date column to datetime dtype a['date'] = pd.to_datetime(a['date']) and plot. 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()

where adjust the formatting as needed such as 在哪里根据需要调整格式,例如

a['date'] = pd.to_datetime(a['date'], format='%Y-%m-%d') 

在此处输入图片说明

II. 二。 Convert colum date to datetime by a = pd.read_csv("test.csv", parse_dates=[0]) 通过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.

相关问题 如何使用 matplotlib.pyplot 在 x 轴上创建分组条形图(按月和年)并在 y 轴上创建值? - How to create a grouped bar chart (by month and year) on the x-axis and values on the y-axis using matplotlib.pyplot? 在PyPlot中绘制纪元列表的直方图,x轴按月 - 年 - Plot histogram of epoch list, x axis by month-year in PyPlot Python:Pandas:Matplotlib:同比(x = 月,y = 每年的捐款) - Python: Pandas: Matplotlib: Year over Year (x = month, y = donations for each year) Python Matplotlib (1) 将 x 轴标签格式化为 Year-Quarter 和 (2) 将 major_locator 设置为月末 - Python Matplotlib (1) format x-axis labels to Year-Quarter and (2) set major_locator to end of month 将多级 X 轴添加到 matplotlib/Seaborn(月和年) - Adding multi level X axis to matplotlib/Seaborn (month and year) 在 python 的 matplotlib.pyplot 中编辑 X 轴 - Editing X-axis in matplotlib.pyplot for python 无法在 pyplot 图表中按月对 x 轴进行分组(Python) - Not able to group x-axis by month in a pyplot chart (Python) Python:使用 matplotlib.pyplot 的奇怪 x 轴限制 - Python: Strange x axis limits using matplotlib.pyplot 在x轴上绘制格式为月年的熊猫数据框索引 - Plot pandas dataframe index formatted as Month-Year on x axis 在 x 轴上创建带有年份月份的 pandas dataframe 的方面 plot - Create facet plot of pandas dataframe with month of year on x-axis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM