python plot 时间_python-matplotlib中的熊猫自动日期时间格式

在第二行中显示年份的一种选择是使用主要和次要刻度线标签.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.dates import MonthLocator, YearLocator, DateFormatter

ix = pd.date_range('1/1/2017', '11/1/2018', freq='D')

vals = np.random.randn(len(ix))

s = pd.DataFrame({'Values': vals}, index=ix)

fig, ax = plt.subplots(figsize=[8,6])

ax.plot(s, lw=1)

ax.xaxis.set_major_locator(YearLocator())

ax.xaxis.set_major_formatter(DateFormatter("

%Y"))

ax.xaxis.set_minor_locator(MonthLocator((1,4,7,10)))

ax.xaxis.set_minor_formatter(DateFormatter("%b"))

plt.show()

如果您需要次要滴答作答,则以下内容将单独格式化主要滴答作响-结果相同.在这里,您将使用FuncFormatter确定取决于月份的格式.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.dates import MonthLocator, DateFormatter

from matplotlib.ticker import FuncFormatter

ix = pd.date_range('1/1/2017', '11/1/2018', freq='D')

vals = np.random.randn(len(ix))

s = pd.DataFrame({'Values': vals}, index=ix)

fig, ax = plt.subplots(figsize=[8,6])

ax.plot(s, lw=1)

monthfmt = DateFormatter("%b")

yearfmt = DateFormatter("%Y")

def combinedfmt(x,pos):

string = monthfmt(x)

if string == "Jan":

string += "

" + yearfmt(x)

return string

ax.xaxis.set_major_locator(MonthLocator((1,4,7,10)))

ax.xaxis.set_major_formatter(FuncFormatter(combinedfmt))

plt.show()

在两种情况下,结果是相同的:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值