用sharey=ax1, 或 sharex
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.collections as collections
import pandas as pd
from pandas import DataFrame,Series
fig=plt.figure()
y = [n*10000 for n in range(0,7)]
ax1 = fig.add_subplot(121)
ax1.ticklabel_format(style='sci', scilimits=(-1,2), axis='x')
ax1.get_xaxis().get_offset_text().set(va='bottom', ha='left')
ax1.xaxis.get_offset_text().set_fontsize(11)
ax1.plot(bot[:25258]['Idle_Min'],color='#A2242F')
ax2 = fig.add_subplot(122,sharey=ax1)
ax2.ticklabel_format(style='sci', scilimits=(-1,2), axis='y')
ax2.get_yaxis().get_offset_text().set(va='bottom', ha='left')
ax2.yaxis.get_offset_text().set_fontsize(11)
ax2.plot(bot[25258:2773492]['Idle_Min'], color='#0C264D')
结果图:
代码里还强制设置x轴使用科学计数法,用scilimits。
另,画子图的方法:
下面这段文字转载自:https://www.cnblogs.com/chenqionghe/p/12355018.html
fig和axis的区别?
相信不少小伙伴一开始都是直接用plt.plot
来绘图,非常简单,但这是偷懒的做法,不建议大家这样。
fig, ax = plt.subplots(2,2)
是比较正统的画法(参数代表行列数),指定figure和axes,然后对axes单独进行操作(图表元素增加和修改)。
fig相当于是一个大的画布,ax相当于是小的子图,一个画布可以有一个或多个子图。
单个图表任何操作都是在axes对象上进行的,包括坐标轴、刻度、图例等。
====(转载到此结束)
比如:
fig, axs = plt.subplots(1, 2, constrained_layout=True)
就是两个子图左右并排的,一行两列
fig, axs = plt.subplots(2, 1)
就是两个子图上下排列,也就是两行一列
plt.subplot(221)
表示两行两列共四个子图,这是第一个子图
具体子图教程看上面的链接,非常详细。