我正在尝试在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
提交
取消