亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Matplotlib:指定刻度標簽的浮點格式

Matplotlib:指定刻度標簽的浮點格式

瀟瀟雨雨 2019-10-30 12:53:13
我正在嘗試在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'))


查看完整回答
反對 回復 2019-10-30
?
白衣染霜花

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。


查看完整回答
反對 回復 2019-10-30
  • 3 回答
  • 0 關注
  • 1681 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號