在matplotlib中以x刻度绘制月份年份 [英] Plotting month year as x ticks in matplotlib

查看:50
本文介绍了在matplotlib中以x刻度绘制月份年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数需要一个月和一年,我想将绘图的xticks设置为输入的月和年.

I have a function that takes a month and year and I want to set the xticks of a plot to the month and year that is inputted.

例如,如果用户输入5作为月份,并输入2019作为年份,则我想将第一个xtick设置为5/2018,第二个为6/2018,依此类推.如果达到12/2018,则下一个刻度应为1/2019.

For example if user enters 5 as the month and 2019 as the year I want to make the first xtick 5/2018, the second one 6/2018, and so on. If it reaches 12/2018, the next tick should be 1/2019.

到目前为止,我的代码:

My code so far:

def cityPlot(self, year, month):
    myDates = [datetime(int(year), month, 1]
    plt.xticks(np.arange(1,17), myDates.split)

我对matplotlib不太熟悉,因此,如果我能找到一些有关如何解决此问题的指导,将会很有帮助.

I'm not very familiar with matplotlib so it would be helpful if I could get some pointers on how to tackle this problem.

推荐答案

有两种方法可以做到这一点.

There's a couple of ways to do this.

请参见下面的一些代码:

See below for some code:

import matplotlib.pyplot as plt
import numpy as np
import datetime

import click

def make_data():
    Nrain       = 20
    start       = datetime.date(2017,12,1)
    end         = datetime.date(2019,1,1)

    period      = (end-start).days/365
    rainfall    = 30*np.random.rand(Nrain) + 120*(1+np.cos(Nrain/period))

    delta       = (end-start)/Nrain
    dates       = [start + i*delta for i in range(Nrain)]
    return rainfall, dates

def plot_rain(rainfall):
    fig = plt.figure(figsize=(8,6))

    ax = fig.subplots()
    ax.plot(rainfall)
    return fig

def xticks_auto(ax,dates,Nticks=10):
    delta       = (dates[-1]-dates[0])/Nticks
    tick_dates  = [dates[0] + i*delta for i in range(Nticks)]
    x_ticks     = ['{}/{}'.format(d.month,d.year) for d in tick_dates]

    ax.set_xticks([i*len(dates)/Nticks for i in range(Nticks)])
    ax.set_xticklabels(x_ticks)

def xticks_pres(ax,dates,Nticks=10):
    start_m     = click.prompt('Start month', type=int)
    start_y     = click.prompt('Start year', type=int)
    end_m       = click.prompt('End month', type=int)
    end_y       = click.prompt('End year', type=int)

    start       = datetime.date(start_y,start_m,1)
    end         = datetime.date(end_y,end_m,1)
    Nticks      = 10
    delta       = (end-start)/Nticks
    tick_dates  = [start + i*delta for i in range(Nticks)]
    x_ticks     = ['{}/{}'.format(d.month,d.year) for d in tick_dates]

    ax.set_xticks([i*len(dates)/Nticks for i in range(Nticks)])
    ax.set_xticklabels(x_ticks)

make_data()生成一些伪降雨数据.如果我们以一些简单的代码开头:

make_data() makes some pseudo-rainfall data. If we run some simple code to begin with:

>>> rainfall, dates = make_data()
>>> fig = plot_rain(rainfall)
>>> ax = fig.axes[0]

这将生成一些数据并将其绘制到图形上

This generates some data and plots it to a figure:

请注意,x值只是数据点的索引.如果我们运行 xticks_pres ,则可以规定开始日期和结束日期,xticks将被更新:

Note that the x-values are simply the indices of the datapoints. If we run xticks_pres, one can prescribe a start date and end date, and the xticks will be updated:

>>> fig = plot_rain(rainfall)
>>> ax = fig.axes[0]
>>> xticks_pres(ax,dates)
Start month: 
5
Start year: 
2011
End month: 
6
End year: 
2015

要添加日期点,您需要日期集的长度或降雨集的长度.如果您设置了日期,则最好使用自动填充:

To be able to add the date points you need either the length of the dates set, or the length of the rainfall set. If you have the dates set, you might as well use an automatic fill:

>>> fig = plot_rain(rainfall)
>>> ax = fig.axes[0]
>>> xticks_auto(ax,dates,5)

在最后的代码执行中,我覆盖了Nticks的默认值10,指定我只想要5个刻度.

On this last code executution, I overrode the defaults value of 10 for Nticks, specifying that I only want 5 ticks.

我认为,使用这些代码片段,您应该能够实现想要实现的目标.如果您确实想使用用户输入,则可以,但是在大多数情况下,简单地自动摇动xticks会更容易.

I think with these snippets you should be able to do what you want to achieve. If you really want to use user input you can, but for most cases it's easier to simply jiggle the xticks automatically.

享受!

这篇关于在matplotlib中以x刻度绘制月份年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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