Matplotlib:指定刻度标签的浮点格式

我正在尝试在matplotlib子图环境中将格式设置为两个十进制数字。不幸的是,我不知道如何解决这个任务。


为了避免在y轴上使用科学计数法,ScalarFormatter(useOffset=False)您可以在下面的代码段中看到。我认为应该通过将其他选项/参数传递给使用的格式化程序来解决我的任务。但是,我在matplotlib的文档中找不到任何提示。


如何设置两位小数位数或不设置两位数(两种情况都需要)?不幸的是,我无法提供样本数据。


-SNIPPET-


f, axarr = plt.subplots(3, sharex=True)


data = conv_air

x = 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_dryer

x = 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_lambda

x = 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)


潇潇雨雨
浏览 1656回答 3
3回答

守候你守候我

一般和具体请参阅相关文档from matplotlib.ticker import FormatStrFormatterfig, ax = plt.subplots()ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

白衣染霜花

如果您直接使用matplotlib的pyplot(plt),并且更熟悉新样式的格式字符串,则可以尝试以下操作:from matplotlib.ticker import StrMethodFormatterplt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) # No decimal placesplt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}')) # 2 decimal places从文档中:matplotlib.ticker.StrMethodFormatter(fmt)类使用新样式的格式字符串(如str.format()所用)来格式化刻度线。用于值的字段必须标记为x,并且用于位置的字段必须标记为pos。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python