【问题标题】:Matplotlib Subplots Unexpected Float Number on X-Y AxesMatplotlib 在 X-Y 轴上绘制意外的浮点数
【发布时间】:2021-01-13 15:23:47
【问题描述】:

我正在使用 Matplotlib 并排绘制两个转置的条形图。但是,我得到了一些意外的浮点数,它们与 x-y 轴上的主要刻度标签重叠。这些浮点数是 0.0、0.2、0.4、0.6、0.8、1.0。其中一些数字与主要的刻度标签重叠。你知道如何去掉这些不需要的小浮点数吗:0.0、0.2、0.4、0.6、0.8、1.0?

这是图片。我在红色框中突出显示了那些不需要的浮点数。

这里是代码。

action_number = [5, 6, 6, 6, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
action_list = ['DrawSword', 'Kiss', 'RideBike', 'Dribble', 'Hug', 'Dive', 
               'Cartwheel', 'Drink', 'Climb', 'Eat', 'Walk', 'FallFloor', 
               'Stand', 'Sword', 'Pick', 'SwingBaseb', 'Handstand', 'ShootBow']

def bar_plot(action_list, action_number, ax):
    y_pos = np.arange(len(action_list))

    ax.barh(y_pos, action_number, height=0.75, align='center', color='b')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(action_list)

    ax.invert_yaxis()

    ax.tick_params(labelsize=12, which='both', axis='both')

    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')

    plt.autoscale(tight=True)
    plt.tight_layout()

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 10))
fig.subplots_adjust(wspace=0, hspace=0)
ax1 = fig.add_subplot(121)
bar_plot(action_list, action_number, ax1)
ax2 = fig.add_subplot(122)
bar_plot(action_list, action_number, ax2)
plt.savefig('action_attribute_sidebyside.pdf',bbox_inches="tight", pad_inches=0)

【问题讨论】:

  • 要筛选的代码很多。您是否尝试过将其逐位删除以查找问题所在?至少,你应该能够想出一个更小的代码块来展示这里
  • ax1.set_yticks(y_pos);ax1.set_yticklabels(action_list)我认为有问题的两行代码就是这两行代码,但是每一行的数据是什么样的?
  • 非常感谢您的帮助。我已经更新了代码和图片。
  • 您正在创建 2 组轴。删除包含fig.add_subplot 的两行,它应该可以工作
  • 感谢您提出的解决方案。效果很好!

标签: python matplotlib


【解决方案1】:

    from matplotlib import pyplot as plt
    import random
    
    action_number = [5, 6, 6, 6, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    action_list = ['DrawSword', 'Kiss', 'RideBike', 'Dribble', 'Hug', 'Dive',
                   'Cartwheel', 'Drink', 'Climb', 'Eat', 'Walk', 'FallFloor',
                   'Stand', 'Sword', 'Pick', 'SwingBaseb', 'Handstand', 'ShootBow']
    
    
    def random_color():
        return "#%06x" % random.randint(0, 0xFFFFFF)
    
    
    colors = [random_color() for x in action_list]
    
    
    def auto_label(horizontal_):
        for index, rectangle in enumerate(horizontal_):
            height = rectangle.get_height()
            width = rectangle.get_width()
            y_value = rectangle.get_y()
            plt.text(width + height/2., y_value, "%d" % width, color=colors[index])
    
    
    plt.subplot(121)
    horizontal = plt.barh(action_list, action_number, edgecolor="k", color=colors)
    auto_label(horizontal)
    
    plt.subplot(122)
    horizontal = plt.barh(action_list, action_number, edgecolor="k", color=colors)
    auto_label(horizontal)
    
    plt.grid()
    
    plt.tight_layout()
    plt.show()

我建议你看看this,这是各种简单的图形和代码示例。

【讨论】:

  • 非常感谢您的帮助。我仍然不知道如何将您的解决方案合并到我的代码中。您能否提供更多提示?谢谢!
  • @ZhenyuWu 请看一下,我已经修改了
  • 这太棒了。我已经盯着并分叉了您出色的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多