【问题标题】:matplotlib naming the tick marks on x axis 1, 2, 4, 8, 16, etcmatplotlib 命名 x 轴 1、2、4、8、16 等上的刻度线
【发布时间】:2013-08-28 19:12:18
【问题描述】:

使用上一个问题中的相同代码,此示例生成以下图表:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

data = (0, 1890,865, 236, 6, 1, 2, 0 , 0, 0, 0 ,0 ,0 ,0, 0, 0)
ind = range(len(data))
width = 0.9   # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, data, width)
plt.xlabel('Duration 2^x')
plt.ylabel('Count')
plt.title('DBFSwrite')
plt.axis([0, len(data), -1, max(data)])

ax = plt.gca()

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

plt.savefig('myfig')

而不是刻度标签是 0、2、4、6、8...我宁愿在每个标记处都标记它们,并继续使用 2^x 的值:1、2、4、8, 16 等。我该怎么做?然后更好的是,我可以让标签在栏下方居中,而不是在左边缘?

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    实现此目的的一种方法是使用LocatorFormatter。这使得可以交互地使用绘图而不会“丢失”刻度线。在这种情况下,我会推荐MultipleLocatorFuncFormatter,如下例所示。

    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from matplotlib.ticker import MultipleLocator, FuncFormatter
    
    data = (0, 1890,865, 236, 6, 1, 2, 0 , 0, 0, 0 ,0 ,0 ,0, 0, 0)
    ind = range(len(data))
    width = 0.9   # the width of the bars: can also be len(x) sequence
    
    # Add `aling='center'` to center bars on ticks
    p1 = plt.bar(ind, data, width, align='center')
    plt.xlabel('Duration 2^x')
    plt.ylabel('Count')
    plt.title('DBFSwrite')
    plt.axis([0, len(data), -1, max(data)])
    
    ax = plt.gca()
    
    # Place tickmarks at every multiple of 1, i.e. at any integer
    ax.xaxis.set_major_locator(MultipleLocator(1))
    # Format the ticklabel to be 2 raised to the power of `x`
    ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: int(2**x)))
    # Make the axis labels rotated for easier reading
    plt.gcf().autofmt_xdate()
    
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    
    plt.savefig('myfig')
    

    【讨论】:

    • @PhillipCloud 我不明白你的评论。
    • @tcaswell 我的错。我以为FuncFormatterMultipleLocator 不是pyplot 的一部分,结果它们是用pyplot 导入的。
    • pyplot 只是一个用于批量导入matplotlib 相关内容的存储桶。
    【解决方案2】:

    xticks() 是你想要的:

    # return locs, labels where locs is an array of tick locations and
    # labels is an array of tick labels.
    locs, labels = xticks()
    
    # set the locations of the xticks
    xticks( arange(6) )
    
    # set the locations and labels of the xticks
    xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
    

    因此,要使 1..4 中 x 的刻度为 2^x,请执行以下操作:

    tick_values = [2**x for x in arange(1,5)]
    
    xticks(tick_values,[("%.0f" % x)  for x in tick_values])
    

    要使标签居中而不是左侧,请在调用 bar 时使用 align='center'

    结果如下:

    【讨论】:

    • +1 顺便说一句,没有必要做[2**x for x in arange(1,5)] 只做2**arange(1, 5) 效率更高。
    • 哦,是的,感谢您指出这一点!只是我爱上了列表推导,所以我尽可能使用它们;)
    • 是的,但你应该更爱 numpy ;)
    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2020-07-26
    • 2020-10-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多