我正在嘗試在matplotlib子圖環境中將格式設置為兩個十進制數字。不幸的是,我不知道如何解決這個任務。為了避免在y軸上使用科學計數法,ScalarFormatter(useOffset=False)您可以在下面的代碼段中看到。我認為應該通過將其他選項/參數傳遞給使用的格式化程序來解決我的任務。但是,我在matplotlib的文檔中找不到任何提示。如何設置兩位小數位數或不設置兩位數(兩種情況都需要)?不幸的是,我無法提供樣本數據。-SNIPPET-f, axarr = plt.subplots(3, sharex=True)data = conv_airx = range(0, len(data))axarr[0].scatter(x, data)axarr[0].set_ylabel('$T_\mathrm{air,2,2}$', size=FONT_SIZE)axarr[0].yaxis.set_major_locator(MaxNLocator(5))axarr[0].yaxis.set_major_formatter(ScalarFormatter(useOffset=False))axarr[0].tick_params(direction='out', labelsize=FONT_SIZE)axarr[0].grid(which='major', alpha=0.5)axarr[0].grid(which='minor', alpha=0.2)data = conv_dryerx = range(0, len(data))axarr[1].scatter(x, data)axarr[1].set_ylabel('$T_\mathrm{dryer,2,2}$', size=FONT_SIZE)axarr[1].yaxis.set_major_locator(MaxNLocator(5))axarr[1].yaxis.set_major_formatter(ScalarFormatter(useOffset=False))axarr[1].tick_params(direction='out', labelsize=FONT_SIZE)axarr[1].grid(which='major', alpha=0.5)axarr[1].grid(which='minor', alpha=0.2)data = conv_lambdax = range(0, len(data))axarr[2].scatter(x, data)axarr[2].set_xlabel('Iterationsschritte', size=FONT_SIZE)axarr[2].xaxis.set_major_locator(MaxNLocator(integer=True))axarr[2].set_ylabel('$\lambda$', size=FONT_SIZE)axarr[2].yaxis.set_major_formatter(ScalarFormatter(useOffset=False))axarr[2].yaxis.set_major_locator(MaxNLocator(5))axarr[2].tick_params(direction='out', labelsize=FONT_SIZE)axarr[2].grid(which='major', alpha=0.5)axarr[2].grid(which='minor', alpha=0.2)
3 回答

守候你守候我
TA貢獻1802條經驗 獲得超10個贊
一般和具體請參閱相關文檔
from matplotlib.ticker import FormatStrFormatter
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

白衣染霜花
TA貢獻1796條經驗 獲得超10個贊
如果您直接使用matplotlib的pyplot(plt),并且更熟悉新樣式的格式字符串,則可以嘗試以下操作:
from matplotlib.ticker import StrMethodFormatter
plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) # No decimal places
plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}')) # 2 decimal places
從文檔中:
matplotlib.ticker.StrMethodFormatter(fmt)類
使用新樣式的格式字符串(如str.format()所用)來格式化刻度線。
用于值的字段必須標記為x,并且用于位置的字段必須標記為pos。
添加回答
舉報
0/150
提交
取消