如何在matplotlib中设置坐标轴的单位长度?

10

例如 x = [1~180,000] 当我绘制它时,在x轴上显示为:1、20,000、40,000... 180,000 这些0很烦人

如何将x轴的单位长度更改为1000,以便显示:1、20、40... 180,并且在某处显示其单位为1000。

我知道我可以自己进行线性变换。但是在matplotlib中有没有函数可以实现这个功能?

2个回答

8

如果你想制作出版物质量的图表,你需要对坐标轴标签进行精细控制。一种方法是提取标签文本并应用自定义格式:

import pylab as plt
import numpy as np

# Create some random data over a large interval
N = 200
X = np.random.random(N) * 10 ** 6
Y = np.sqrt(X)

# Draw the figure to get the current axes text
fig, ax = plt.subplots()
plt.scatter(X,Y)
ax.axis('tight')
plt.draw()

# Edit the text to your liking
label_text   = [r"$%i \cdot 10^4$" % int(loc/10**4) for loc in plt.xticks()[0]]
ax.set_xticklabels(label_text)

# Show the figure
plt.show()

enter image description here


3
你可以使用pyplot.ticklabel_format将标签样式设置为科学计数法。
import pylab as plt
import numpy as np

# Create some random data over a large interval
N = 200
X = np.random.random(N) * 10 ** 6
Y = np.sqrt(X)

# Draw the figure to get the current axes text
fig, ax = plt.subplots()
plt.scatter(X,Y)
ax.axis('tight')
plt.draw()

plt.ticklabel_format(style='sci',axis='x',scilimits=(0,0))

# Edit the text to your liking
#label_text   = [r"$%i \cdot 10^4$" % int(loc/10**4) for loc in plt.xticks()[0]]
#ax.set_xticklabels(label_text)

# Show the figure
plt.show()

Output


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接