plot_date、plot_step、plot_log

1,814 阅读3分钟

这里的plot_date是pyplot的一个函数不是matplotlib的date库。

plot_date

Axes.plot_date(self, x, y, fmt=’o’, tz=None, xdate=True, ydate=False,*, data=None,**kwargs)

selt:python函数自带参数

x,y:x轴与y轴数据。根据后面的xdate与ydate的布尔值决定是否要转化为matplotlib date形式。也可以直接给出matlotlib date形式参数(matplot date库的内容)。

fmt:曲线或者点的格式,与plot的参数fmt一样。

tz:时区参数。可以是一个表示时区的字符串或者是一个时区对象,或者省略。省略的话会默认是rc文件里面的timezone参数的值,默认是UTC。

xdate,ydate:布尔值,如果为真的话,x或者y的值会被自动转化为matplotlib date形式。

这里牵涉到太多的matplotlib date,dateutil的知识,所以就简单用官方的一个实例来说明。


"""===============
Date Demo Rrule
===============
Show how to use an rrule instance to make a custom date ticker - here
we put a tick mark on every 5th easterSee 
https://dateutil.readthedocs.io/en/stable/ for help with rrules"""
import matplotlib.pyplot as plt
from matplotlib.dates import (YEARLY, DateFormatter,
                              rrulewrapper, RRuleLocator, drange)
import numpy as npimport datetime
# Fixing random state for reproducibility
np.random.seed(19680801)
# tick every 5th easte
rrule = rrulewrapper(YEARLY, byeaster=1, interval=5)
loc = RRuleLocator(rule)
formatter = DateFormatter('%m/%d/%y')
date1 = datetime.date(1952, 1, 1)
date2 = datetime.date(2004, 4, 12)
delta = datetime.timedelta(days=100)
dates = drange(date1, date2, delta)
s = np.random.rand(len(dates))  
# make up some random y values
fig, ax = plt.subplots()
plt.plot_date(dates, s)
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.set_tick_params(rotation=30, labelsize=10)
plt.show()

rrulewrapper:设置rruler参数。关于rruler,可以参考dateutil.rruler,这里是设置产生时间序列的参数,YEARLY是按照年份来产生,byeaster参数是设置筛选距离复活节多少天的日期,如果为1就是表示筛选复活节,interval表示筛选间隔,这里表示每个两年选择一个时间。

RRuleLocator:设置轴上时间的位置,传入上面的rrule对象表示坐标轴上面放置的是上面的时间序列。

DateFormatter:时间格式,'%m%d%y'表示月天年(MDY)

timedeltadrange:产生时间序列,作为x

plot_step

绘制曲线的阶梯图像

Axes.step(self, x, y,*args, where=’pre’, data=None,**kwargs)


x、y、data、非关键字参数以及后面的**kwarg都和plot相同。主要说明一下where参数。

where:可选参数,{'pre','post','mid'}。定义阶梯的取值。先看一个例子。

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(14)y = np.sin(x / 2)
plt.step(x, y + 2, label='pre (default)')
plt.plot(x, y + 2, 'o--', color='grey', alpha=0.3)
plt.step(x, y + 1, where='mid', label='mid')
plt.plot(x, y + 1, 'o--', color='grey', alpha=0.3)
plt.step(x, y, where='post', label='post')
plt.plot(x, y, 'o--', color='grey', alpha=0.3)
plt.grid(axis='x', color='0.95')
plt.legend(title='Parameter where:')
plt.title('plt.step(where=...)')
plt.show()


'pre':在x[i-1]与x[i]之间取y[i]。

'post':在x[i-1]与x[i]之间取y[i-1]。

'mid':阶梯只出现在两个x之间的一半路程。

plot_log

这个函数的参数与plot有相似的参数,也有自己的专属参数。

函数用于指数对数形式的坐标轴。

参数

basex,basey:x轴或者y轴的底数。可选参数,为一个标量。

subsx,subsy:在Axes.set_xscale / Axes.set_yscale里面作为参数,设置坐标轴样式。

nonposx, nonposy:可选参数,{'mask','clip'},默认为mask。mask表示非正数会被标记为无效,clipped会把这些数变成很小的正数。

import matplotlib.pyplot as plt

import numpy as np

t = np.arange(0.01, 20.0, 0.01)

fig=plt.figure()

ax=fig.gca()

ax.loglog(t, 20 * np.exp(-t / 10.0),basex=2)

ax.set(title='loglog base 2 on x')

ax.grid()