python 子图+每个都是双y轴

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


#调整画布大小
fig = plt.figure(figsize=(16,2))
#调整子图间距
plt.subplots_adjust(left=None, bottom=None, right=None, top=None,
                wspace=0.38, hspace=0)

#第一张子图
ax1 = plt.subplot(1,4,1)
ax1.plot(np.linspace(1,20,10),np.linspace(1,20,10),linewidth=2,marker="o",markersize=2,markerfacecolor="white",markeredgecolor="black",color='black',label='Threthold point')
ax1.set_ylabel('value')
#第二个y轴
ax2 = ax1.twinx()
ax2.plot(np.linspace(1,20,10),[2 for i in range(10)],linewidth=2,marker="o",markersize=2,
         markerfacecolor="white",markeredgecolor="blue",color='blue',label='Std')
ax2.tick_params(axis='y', colors='blue')
ax2.spines["right"].set_color("blue")# 修改右边颜色



#第一张子图
ax3 = plt.subplot(1,4,2)
ax3.plot(np.linspace(1,20,10),np.linspace(1,20,10),linewidth=2,marker="o",markersize=2,markerfacecolor="white",markeredgecolor="black",color='black',label='Threthold point')
ax3.set_ylabel('value')
#第二个y轴
ax4 = ax3.twinx()
ax4.plot(np.linspace(1,20,10),[2 for i in range(10)],linewidth=2,marker="o",markersize=2,
         markerfacecolor="white",markeredgecolor="blue",color='blue',label='Std')
ax4.tick_params(axis='y', colors='blue')
ax4.spines["right"].set_color("blue")


#第一张子图
ax5 = plt.subplot(1,4,3)
ax5.plot(np.linspace(1,20,10),np.linspace(1,20,10),linewidth=2,marker="o",markersize=2,markerfacecolor="white",markeredgecolor="black",color='black',label='Threthold point')
ax5.set_ylabel('value')
#第二个y轴
ax6 = ax5.twinx()
ax6.plot(np.linspace(1,20,10),[2 for i in range(10)],linewidth=2,marker="o",markersize=2,
         markerfacecolor="white",markeredgecolor="blue",color='blue',label='Std')
ax6.tick_params(axis='y', colors='blue')
ax6.spines["right"].set_color("blue")


#第一张子图
ax7 = plt.subplot(1,4,4)
ax7.plot(np.linspace(1,20,10),np.linspace(1,20,10),linewidth=2,marker="o",markersize=2,markerfacecolor="white",markeredgecolor="black",color='black',label='Threthold point')
ax7.set_ylabel('value')
#第二个y轴
ax8 = ax7.twinx()
ax8.plot(np.linspace(1,20,10),[2 for i in range(10)],linewidth=2,marker="o",markersize=2,
         markerfacecolor="white",markeredgecolor="blue",color='blue',label='Std')
ax8.tick_params(axis='y', colors='blue')
ax8.spines["right"].set_color("blue")

plt.rc('ytick', labelsize=8)
ax2.set_xlabel("sub1")
ax4.set_xlabel("sub2")
ax6.set_xlabel("sub3")
ax8.set_xlabel("sub4")
#子标题
ax1.set_title("sub1",size=11)
ax3.set_title("sub2",size=11)
ax5.set_title("sub3",size=11)
ax7.set_title("sub4",size=11)
fig.legend(bbox_to_anchor=(0.275,0.88),ncol=1,fontsize=7,frameon=False)
#边框加粗
ax8.spines['bottom'].set_linewidth(1.5)
ax8.spines['right'].set_linewidth(1.5)
ax8.spines['left'].set_linewidth(1.5)
ax8.spines['top'].set_linewidth(1.5)
plt.show()

猜你喜欢

转载自blog.csdn.net/qq_44785318/article/details/126287534
今日推荐