如何在matplotlib

时间:2017-09-25 21:05:57

标签: python matplotlib scale yaxis

我有以下代码使用Jupyter在Pytho中创建一组2 x 2图。问题是第四个图(左下)的左y标签与第三个图的右y标签碰撞。我试图将第四个图的左侧y标签的比例更改为数千,所以不是显示-1000,0,1000等,而是只需要给鞋子1,0,1,然后给予足够的标题空间。我怎么能这样做?

关注我的代码:

    #Testing 2 x 2 plots for FO reporting

plt.figure(figsize=(15,9))

#First Plot

ax1=plt.subplot(2,2,1)
plt.title("Downhole Pressure")
ax1.plot(Time, Downhole_pressure, 'b', markersize=2, label="Downhole pressure")
plt.xlabel("Injection time", fontsize=10)
plt.xlim(0,80)
plt.ylabel("Pressure, (psi)", fontsize=10)
plt.ylim(6500,7500)
plt.legend(bbox_to_anchor=(.999,.89), prop={'size': 10})

ax2=ax1.twinx()
ax2.plot(Time, Slurry_rate,'m', markersize=2, label="Slurry")
ax2.plot(Time, Proppant_rate, 'g', markersize=2, label="Proppant")
plt.ylabel("Injection rate, (BPM))", fontsize=10)
plt.ylim(0,100)

plt.legend(bbox_to_anchor=(.879,.825), prop={'size': 10})

#Second Plot

ax1=plt.subplot(2,2,2)
plt.title("Fracture Width")
ax1.plot(Time, Max_Frac_Witdth, 'b', markersize=2, label="Max")
ax1.plot(Time, Mean_Frac_Width, 'r', markersize=2, label="Mean")
plt.xlabel("Injection time", fontsize=10)
plt.xlim(0,80)
plt.ylabel("Fracture width, (mm)", fontsize=10)
plt.ylim(0,10)
plt.legend(bbox_to_anchor=(.84,.89), prop={'size': 10})

ax2=ax1.twinx()
ax2.plot(Time, Slurry_rate,'m', markersize=2, label="Slurry")
ax2.plot(Time, Proppant_rate, 'g', markersize=2, label="Proppant")
plt.ylabel("Injection rate, (BPM))", fontsize=10)
plt.ylim(0,100)
plt.legend(bbox_to_anchor=(.879,.775), prop={'size': 10})

#Third Plot

ax1=plt.subplot(2,2,3)
plt.title("Fracture Height")
ax1.plot(Time, Max_Frac_Ver_Pos, 'b', markersize=2, label="Max")
ax1.plot(Time, Min_Frac_Ver_Pos, 'r', markersize=2, label="Min")
ax1.plot(Time, Frac_Ver_Height, 'darkblue', markersize=2, label="Fracture Vertical Height")
plt.xlabel("Injection time", fontsize=10)
plt.xlim(0,80)
plt.ylabel("Fracture Vertical Position (ft)", fontsize=10)
plt.ylim(-200,400)
plt.legend(bbox_to_anchor=(.99,.6), prop={'size': 10})

ax2=ax1.twinx()
ax2.plot(Time, Slurry_rate,'m', markersize=2, label="Slurry")
ax2.plot(Time, Proppant_rate, 'g', markersize=2, label="Proppant")
plt.ylabel("Injection rate, (BPM))", fontsize=10)
plt.ylim(0,100)
plt.legend(bbox_to_anchor=(.82,.775), prop={'size': 10})

#Fourth Plot

ax1=plt.subplot(2,2,4)
plt.title("Fracture Length")
ax1.plot(Time, Max_Frac_Trans_Pos, 'b', markersize=2, label="Max")
ax1.plot(Time, Min_Frac_Trans_Pos, 'r', markersize=2, label="Min")
ax1.plot(Time, Frac_Trans_Length, 'darkblue', markersize=2, label="Fracture Transversal Length")
plt.xlabel("Injection time", fontsize=10)
plt.xlim(0,80)
plt.ylabel("Fracture Transversal Position (ft)", fontsize=10)

plt.ticklabel_format(style='sci', axis='x', scilimits=(-2e3,3e3)) #tried, didn't work
#plt.ylim(-2e3,3e3) #tried, didn't work

plt.legend(bbox_to_anchor=(.99,.6), prop={'size': 10})

ax2=ax1.twinx()
ax2.plot(Time, Slurry_rate,'m', markersize=2, label="Slurry")
ax2.plot(Time, Proppant_rate, 'g', markersize=2, label="Proppant")
plt.ylabel("Injection rate, (BPM))", fontsize=10)
plt.ylim(0,100)
plt.legend(bbox_to_anchor=(.82,.775), prop={'size': 10})

plt.savefig('side-by-side_09.png', dpi=300)
#plt.show()
plt.gcf().clear()
plt.close('all')

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

要为标题留出足够的位置,可以添加行

plt.tight_layout()

保存图之前。它将优化轴的位置。

要更改轴的偏移量,以便yticks显示1,-1,请使用

plt.ticklabel_format(useOffset=1000)