繁体   English   中英

在matplotlib中选择x值的比例

[英]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.

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