简体   繁体   English

在matplotlib中选择x值的比例

[英]choose scale of x values in matplotlib

I am trying to create a histogram using matplotlib. 我正在尝试使用matplotlib创建直方图。 my X values double, from 2 - 2048. When I graph this on a histogram, there are a bunch of values right next to each other at the start, then it spreads out as the values go up. 我的X值加倍,从2到2048.当我在直方图上绘制图形时,在开始时有一堆值彼此相邻,然后随着值的增加而展开。 Since I know I will not have values in between, How can I change the graph to it steps up by a multiple of two? 因为我知道我之间不会有值,如何将图形更改为两倍的倍数? I have tried from matplotlib.pyplot import xticks but that only changes what is displayed. 我试过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()

You can just plot the log values, and the log of the formatters. 您可以只绘制日志值和格式化程序的日志。 It is much simpler ... 这是简单...

plt.hist(np.log(x), bins=50, normed=True)
plt.xticks(np.log(z), z)

You can also use a base 2 log if you wish ... 如果您愿意,也可以使用base 2日志...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM