如何使用`set_major_locator`将x-ticks设置为月份? [英] How to set x-ticks to months with `set_major_locator`?

查看:31
本文介绍了如何使用`set_major_locator`将x-ticks设置为月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将 x-ticks 设置为[Jan.,Feb.,...]

 将matplotlib.pyplot导入为plt从matplotlib.dates导入MonthLocator,DateFormatterfig = plt.figure(figsize=[10, 5])ax = fig.add_subplot(111)ax.plot(np.arange(1000))ax.xaxis.set_major_locator(MonthLocator())ax.xaxis.set_major_formatter(DateFormatter('%b'))

我得到下图,没有x勾号

我想知道为什么所有的X-ticks都消失了?我参考

请注意,如果您不想绘制每个月,您可以通过删除主要定位器让 matplotlib 为您处理.

  fig = plt.figure(figsize = [10,5])ax = fig.add_subplot(111)ax.plot(your_df ['vals'])plt.xticks(rotation='vertical')# ax.xaxis.set_major_locator(MonthLocator())ax.xaxis.set_major_formatter(DateFormatter('%b'))

已添加 进入提供的链接,您在使用的数据集 (boulder-precip.csv) 中有一个 DATE 字段.您实际上可以遵循相同的过程,并按月进行绘制:

df = pd.read_csv('boulder-precip.csv')df ['DATE'] = pd.to_datetime(df ['DATE'])df = df.set_index('DATE')fig = plt.figure(figsize=[10, 5])ax = fig.add_subplot(111)ax.plot(df['PRECIP'])plt.xticks(rotation='vertical')ax.xaxis.set_major_locator(MonthLocator())ax.xaxis.set_major_formatter(DateFormatter('%b'))

I am trying to use the following code to set the x-ticks to [Jan., Feb., ...]

import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, DateFormatter
fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111)
ax.plot(np.arange(1000))
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))

I get the following figure, without x-ticks

I'm wondering why all x-ticks disappeared? I wrote the above code with reference to this implementation

Many thanks.

解决方案

It is not very clear the type of data you currently have. But below are my suggestions for plotting the month on the x-axis:

  1. Transform your date using pd.to_datetime
  2. Set it to your dataframe index.
  3. Call explicitly the plt.set_xticks() method

Below one example with re-created data:

from datetime import datetime as dt
from datetime import timedelta

### create sample data
your_df = pd.DataFrame()
your_df['vals'] = np.arange(1000)

## make sure your datetime is considered as such by pandas
your_df['date'] = pd.to_datetime([dt.today()+timedelta(days=x) for x in range(1000)])
your_df=  your_df.set_index('date') ## set it as index

### plot it
fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111)
ax.plot(your_df['vals'])
plt.xticks(rotation='vertical')
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))

Note that if you do not want every month plotted, you can let matplotlib handle that for you, by removing the major locator.

fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111)
ax.plot(your_df['vals'])
plt.xticks(rotation='vertical')
# ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))

Added Went into the link provided, and you do have a DATE field in the dataset used (boulder-precip.csv). You can actually follow the same procedure and have it plotted on a monthly-basis:

df = pd.read_csv('boulder-precip.csv')
df['DATE'] = pd.to_datetime(df['DATE'])
df = df.set_index('DATE')

fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111)
ax.plot(df['PRECIP'])
plt.xticks(rotation='vertical')
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))

这篇关于如何使用`set_major_locator`将x-ticks设置为月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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