[英]How to set the unit length of axis in matplotlib?
例如x = [1~180,000]當我繪制它時,在x軸上,它顯示:1,20,000,40,000,... 180,000這些0真的很煩人
如何將x軸的單位長度更改為1000,以便顯示:1,20,40,... 180並顯示其單位為1000的某個位置。
我知道我自己可以進行線性轉換。 但是在matplotlib中是不是有一個函數呢?
如果您的目標是制作出版物質量數據,則需要對軸標簽進行精細控制。 一種方法是提取標簽文本並應用您自己的自定義格式:
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()
您可以使用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.