Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib:指定刻度标签的浮动格式_Python_Python 3.x_Matplotlib_Scipy - Fatal编程技术网

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

Python Matplotlib:指定刻度标签的浮动格式,python,python-3.x,matplotlib,scipy,Python,Python 3.x,Matplotlib,Scipy,我试图在matplotlib子地块环境中将格式设置为两个十进制数。不幸的是,我不知道如何解决这项任务 为了防止在y轴上使用科学符号,我使用了ScalarFormatter(useOffset=False),您可以在下面的代码片段中看到这一点。我认为我的任务应该通过向使用的格式化程序传递更多选项/参数来解决。然而,我在matplotlib的文档中找不到任何提示 如何设置两个十进制数字或无(两种情况都需要)?很遗憾,我无法提供样本数据 --片段-- 请参阅和中的相关文档 上面的答案可能是正确的方法

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

为了防止在y轴上使用科学符号,我使用了ScalarFormatter(useOffset=False),您可以在下面的代码片段中看到这一点。我认为我的任务应该通过向使用的格式化程序传递更多选项/参数来解决。然而,我在matplotlib的文档中找不到任何提示

如何设置两个十进制数字或无(两种情况都需要)?很遗憾,我无法提供样本数据


--片段--


请参阅和中的相关文档


上面的答案可能是正确的方法,但对我不起作用

对我来说,解决这个问题的方法如下:

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()
ax=
#获取当前标签
labels=[ax.get\uxticklabels()中项目的item.get\uText()
#击败他们,让他们屈服,让他们再次倒退
ax.为标签中的标签设置标签([str(圆形(浮动(标签),2)))
#展示情节,回家与家人团聚
plt.show()

如果您直接使用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


在matplotlib 3.1中,还可以使用。要防止无偏移的科学记数法:

plt.gca().ticklabel_format(axis='both', style='plain', useOffset=False)
使用lambda函数格式化标签 3倍相同的绘图,y标记不同

最小示例

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))
当然,您也可以使用“实”函数而不是lambda。

注意:如果您更喜欢使用新的
.format()
样式说明符,您可以使用我正在使用
imshow
进行绘图中提到的
StrMethodFormatter
,这对我不适用。我还尝试了来自的
plt.gca().yaxis.set\u major\u格式化程序(FormatStrFormatter('.g'))
,但没有效果。有什么想法吗?@airdas如果遇到问题,请提出新问题,提供问题的所有细节和答案。是否可以在同一轴上使用两种不同格式的记号?我正在插入一些额外的勾号,我希望它是
.2f
,但我希望原始的勾号是
.1f
。甚至可以在不单独导入
FormatStrFormatter
的情况下工作。其位于
pyplot
导入matplotlib.pyplot作为plt下
ax.yaxis.set\u major\u formatter(plt.FormatStrFormatter('%.2f'))
如果标签!='''],您应该将标签的
[str(round(float(label),2)]放在标签中,否则您将遇到空标签的问题。
plt.gca().ticklabel_format(axis='both', style='plain', useOffset=False)
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))