Matplotlib绘图双纵坐标轴设置及控制设置时间格式

478 查看

双y轴坐标轴图

今天利用matplotlib绘图,想要完成一个双坐标格式的图。

横坐标设置时间间隔

纵坐标设置显示百分比

知识点

在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个,或者多个Axes对象。每个Axes对象都是一个拥有自己坐标系统的绘图区域。其逻辑关系如下:

一个Figure对应一张图片。

Title为标题。Axis为坐标轴,Label为坐标轴标注。Tick为刻度线,Tick Label为刻度注释。1

Title为标题。Axis为坐标轴,Label为坐标轴标注。Tick为刻度线,Tick Label为刻度注释。

add_subplot()

The Axes instance will be returned.

twinx()

create a twin of Axes for generating a plot with a sharex x-axis but independent y axis. The y-axis of self will have ticks on left and the returned axes will have ticks on the right.
意思就是,创建了一个独立的Y轴,共享了X轴。双坐标轴!

类似的还有twiny()

ax1.xaxis.set_major_formatter

Set the formatter of the major ticker
ACCEPTS: A Formatter instance

DateFormatter()

strftime方法(传入格式化字符串)。

FormatStrFormatter()

Use a new-style format string (as used by str.format()) to format the tick. The field formatting must be labeled x
定义字符串格式。

plt.xticks

代码汇总