簡體   English   中英

如何在matplotlib中將y軸設置為千位

[英]How to set y-axis to Thousands in matplotlib

我有以下代碼使用 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')

在此先感謝您的幫助!

為了讓標題有足夠的位置,您可以添加以下行

plt.tight_layout()

在保存圖形之前。 它將優化軸的位置。

要更改軸的偏移量,以便 yticks 顯示 1, -1,請使用

plt.ticklabel_format(useOffset=1000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM