[英]choose scale of x values in matplotlib
我正在嘗試使用matplotlib創建直方圖。 我的X值加倍,從2到2048.當我在直方圖上繪制圖形時,在開始時有一堆值彼此相鄰,然后隨着值的增加而展開。 因為我知道我之間不會有值,如何將圖形更改為兩倍的倍數? 我試過from matplotlib.pyplot import xticks
但只改變顯示的內容。
import matplotlib
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import random
from matplotlib.pyplot import xticks
def to_percent(y, position):
# Ignore the passed in position. This has the effect of scaling the default
# tick locations.
s = str(100 * y)
# The percent symbol needs escaping in latex
if matplotlib.rcParams['text.usetex'] is True:
return s + r'$\%$'
else:
return s + '%'
z = [2, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
x = [z[random.randint(0, 6)] for i in range(100)]
xticks(z)
plt.xscale('log')
# Make a normed histogram. It'll be multiplied by 100 later.
plt.hist(x, bins=50, normed=True)
# Create the formatter using the function to_percent. This multiplies all the
# default labels by 100, making them all percentages
formatter = FuncFormatter(to_percent)
# Set the formatter
plt.gca().yaxis.set_major_formatter(formatter)
plt.show()
您可以只繪制日志值和格式化程序的日志。 這是更簡單...
plt.hist(np.log(x), bins=50, normed=True)
plt.xticks(np.log(z), z)
如果您願意,也可以使用base 2日志...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.