【问题标题】:change format values of the axis plots in matplotlib更改 matplotlib 中轴图的格式值
【发布时间】:2017-10-17 18:38:07
【问题描述】:

我是 matplotlib 的新手,我想更改轴 x 的值格式,如下所示:

轴 x : 250000, 500000 等 改成 0.250, 0.500 等格式

为此我能做些什么?

谢谢

【问题讨论】:

标签: matplotlib format decimal axis


【解决方案1】:

您通常只是在绘图之前缩放数据。所以你可以使用plt.plot(x,y/1e6),而不是plt.plot(x,y)。

要格式化带有 3 位小数的值,请使用 matplotlib.ticker.StrMethodFormatter 并提供带有 3 位小数的格式,在本例中为 "{x:.3f}"。

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np; np.random.seed(42)

x = np.arange(5)
y = np.array([5e5,2e5,0,3e5,4e5])

plt.plot(x,y/1e6)

plt.gca().yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:.3f}"))

plt.show()

【讨论】:

  • 谢谢,我做了: def numfmt(x, pos): # 你的自定义格式化函数:除以 100.0 s = '{}'.format(x / 1000000.0) return s ;和 xfmt = tkr.FuncFormatter(numfmt) plt.gca().xaxis.set_major_formatter(xfmt)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2017-10-13
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多