X和Y轴的测量长度 [英] Measurement length for X and Y-axis

查看:52
本文介绍了X和Y轴的测量长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以更改由熊猫创建的图形的度量里程碑.在我的代码中,X 轴代表时间,按月测量,但测量里程碑无处不在.

在下图中,X轴的里程碑是2012M012012M062012M112013M04> 和 2013M09.

有什么办法可以选择每个里程碑之间的距离吗?例如,要使其每年或每半年显示一次?

这是我用于制作图表的函数的代码:

def 图(数据框):图= dataframe [["Profit"]].plot()graph.set_title('统计')graph.set_ylabel('千美元')graph.set_xlabel('时间')plt.grid(真)plt.show()

实际的数据框只是一个包含大量月份和货币价值的 excel 文件.

解决方案

我认为最直接的方法是使用 matplotlib.dates 格式化轴:

将pandas导入为pd将numpy导入为np导入matplotlib.pyplot作为plt导入 matplotlib.dates 作为 mdates清晰度图(数据框):无花果,ax = plt.subplots()xfmt = mdates.DateFormatter('%YM%m')#请参阅https://strftime.org/Major = mdates.MonthLocator([1,7]) #仅标记一月和七月graph = dataframe[["Profit"]].plot(ax=ax) #将绘图链接到现有轴graph.set_title('统计')graph.set_ylabel('千美元')graph.set_xlabel('时间')graph.xaxis.set_major_locator(major)#在x轴上设置主要定位器刻度graph.xaxis.set_major_formatter(xfmt) #format xtick 标签plt.grid(真)plt.show()

但是一个关键点是您需要将日期作为Python的内置 datetime.date (不是 datetime.datetime );感谢

<小时>

只是为了扩展一下,这里是当索引是 pandas.Timestamp 而不是 datetime.date 时会发生什么:

在[0]:dr = pd.date_range('01 -01-2012','01 -01-2014',freq ='1MS')#dr = [df.index中日期的pd.to_datetime(date).date()]#跳过日期转换df = pd.DataFrame(index = dr,data = {'Profit':np.random.rand(25)})图(df)出[0]:

x 轴格式不正确:

但是,如果您只想直接通过 matplotlib 而不是 pandas ( pandas 使用matplotlib 无论如何),这可以处理更多类型的日期:

在[0]:dr = pd.date_range('01 -01-2012','01 -01-2014',freq ='1MS')# dr = [pd.to_datetime(date).date() for date in df.index] #跳过日期转换df = pd.DataFrame(index=dr, data={'Profit':np.random.rand(25)})def graph_2(数据框):无花果,ax = plt.subplots()xfmt = mdates.DateFormatter('%YM%m')主要 = mdates.MonthLocator([1,7])ax.plot(dataframe.index,dataframe ['Profit'],label ='Profit')ax.set_title('统计')ax.set_ylabel('千美元')ax.set_xlabel('时间')ax.xaxis.set_major_locator(major)ax.xaxis.set_major_formatter(xfmt)ax.legend() #legend 需要添加plt.grid(真)plt.show()图_2(df)类型(df.index[0])出[0]:pandas._libs.tslibs.timestamps.Timestamp

这是工作图:

I wonder if it's possible to change the measurement milestones for graphs created by pandas. In my code the X-axis stands for time and is measured by month, but the measurement milestones are all over the place.

In the image below, the milestones for the X-axis are 2012M01, 2012M06, 2012M11, 2013M04 and 2013M09.

Is there any way I can choose how long the distance should be between every milestone? For example, to make it so it shows every year or every half year?

This is the code I used for the function making the graph:

def graph(dataframe):
    graph = dataframe[["Profit"]].plot() 
    graph.set_title('Statistics')
    graph.set_ylabel('Thousand $')
    graph.set_xlabel('Time')
    plt.grid(True)          
    plt.show()

The actual dataframe is just an excel-file with a bunch of months and monetary values in it.

解决方案

I think the most straight forward is to use matplotlib.dates to format the axis:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def graph(dataframe):
    fig, ax = plt.subplots()
    xfmt = mdates.DateFormatter('%YM%m')            #see https://strftime.org/
    major = mdates.MonthLocator([1,7])              #label only Jan and Jul

    graph = dataframe[["Profit"]].plot(ax=ax)       #link plot to the existing axes
    graph.set_title('Statistics')
    graph.set_ylabel('Thousand $')
    graph.set_xlabel('Time')
    graph.xaxis.set_major_locator(major)            #set major locator tick on x-axis
    graph.xaxis.set_major_formatter(xfmt)           #format xtick label
    plt.grid(True)          
    plt.show()

But a key point is you need to have your dates as Python's built-in datetime.date (not datetime.datetime); thanks to this answer. If your dates are str or a different type of datetime, you will need to convert, but there are many resources on SO and elsewhere for doing this like this or this:

In[0]:

dr = pd.date_range('01-01-2012', '01-01-2014', freq='1MS')
dr = [pd.to_datetime(date).date() for date in df.index]    #explicitly converting to datetime with .date()

df = pd.DataFrame(index=dr, data={'Profit':np.random.rand(25)})
type(df.index.[0])

Out[0]:
datetime.date

Calling graph(df) using the example above gets this plot:


Just to expand on this, here's what happens when the index is pandas.Timestamp instead of datetime.date:

In[0]:
dr = pd.date_range('01-01-2012', '01-01-2014', freq='1MS')
# dr = [pd.to_datetime(date).date() for date in df.index]       #skipping date conversion
df = pd.DataFrame(index=dr, data={'Profit':np.random.rand(25)})

graph(df)

Out[0]:

The x-axis is improperly formatted:

However, if you are willing to just create the plot directly through matplotlib, rather than pandas (pandas is using matplotlib anyway), this can handle more types of dates:

In[0]:
dr = pd.date_range('01-01-2012', '01-01-2014', freq='1MS')
# dr = [pd.to_datetime(date).date() for date in df.index]         #skipping date conversion
df = pd.DataFrame(index=dr, data={'Profit':np.random.rand(25)})

def graph_2(dataframe):
    fig, ax = plt.subplots()
    xfmt = mdates.DateFormatter('%YM%m')
    major = mdates.MonthLocator([1,7])

    ax.plot(dataframe.index,dataframe['Profit'], label='Profit')
    ax.set_title('Statistics')
    ax.set_ylabel('Thousand $')
    ax.set_xlabel('Time')
    ax.xaxis.set_major_locator(major)
    ax.xaxis.set_major_formatter(xfmt)
    ax.legend()                          #legend needs to be added
    plt.grid(True)          
    plt.show()

graph_2(df)
type(df.index[0])

Out[0]:

pandas._libs.tslibs.timestamps.Timestamp

And here is the working graph:

这篇关于X和Y轴的测量长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆