alb*_*ert 43 python matplotlib scipy python-3.x
我试图在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)
Run Code Online (Sandbox Code Playgroud)
tac*_*ell 84
from matplotlib.ticker import FormatStrFormatter
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
Run Code Online (Sandbox Code Playgroud)
sap*_*ico 16
上面的答案可能是正确的方法,但对我不起作用.
解决它的hacky方式如下:
ax = <whatever your plot is>
# get the current labels
labels = [item.get_text() for item in ax.get_xticklabels()]
# Beat them into submission and set them back again
ax.set_xticklabels([str(round(float(label), 2)) for label in labels])
# Show the plot, and go home to family
plt.show()
Run Code Online (Sandbox Code Playgroud)
Mar*_*hke 11
最小的例子
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt
from matplotlib.ticker import FormatStrFormatter
fig, axs = mpl.pylab.subplots(1, 3)
xs = np.arange(10)
ys = 1 + xs ** 2 * 1e-3
axs[0].set_title('default y-labeling')
axs[0].scatter(xs, ys)
axs[1].set_title('custom y-labeling')
axs[1].scatter(xs, ys)
axs[2].set_title('x, pos arguments')
axs[2].scatter(xs, ys)
fmt = lambda x, pos: '1+ {:.0f}e-3'.format((x-1)*1e3, pos)
axs[1].yaxis.set_major_formatter(mpl.ticker.FuncFormatter(fmt))
fmt = lambda x, pos: 'x={:f}\npos={:f}'.format(x, pos)
axs[2].yaxis.set_major_formatter(mpl.ticker.FuncFormatter(fmt))
Run Code Online (Sandbox Code Playgroud)
当然,您也可以使用“真实”函数来代替 lambda。 https://matplotlib.org/3.1.1/gallery/ticks_and_spines/tick-formatters.html
如果您直接使用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
Run Code Online (Sandbox Code Playgroud)
从文档中:
matplotlib.ticker.StrMethodFormatter(fmt)类
使用新样式的格式字符串(如str.format()所用)来格式化刻度线。
用于值的字段必须标记为x,并且用于位置的字段必须标记为pos。
在 matplotlib 3.1 中,您还可以使用ticklabel_format。为了防止科学记数法没有偏移量:
plt.gca().ticklabel_format(axis='both', style='plain', useOffset=False)
Run Code Online (Sandbox Code Playgroud)