【问题标题】:Why is the hatch not showing?为什么没有显示舱口?
【发布时间】:2020-10-15 04:08:19
【问题描述】:

我正在 matplotlib 中测试一个程序,它将改变条形的颜色和阴影。但是,当我使用.set_color() 方法时,它看起来与条的颜色相同。

我尝试了什么

我查找了如何更改阴影颜色,发现您可以使用 .set_hatch_color() 更改它,我在文档中没有找到。

这是错误信息:

AttributeError: 'Rectangle' object has no attribute 'set_hatch_color'

这是我的代码:

import matplotlib.pyplot as plt

teams = ["Brazil", "Uruguay", "Peru"]
points = [6, 3, 1]

bars = plt.bar(teams, points)

bars[0].set_color("r")
bars[0].set_hatch("/")

plt.show()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    调用set_color 设置内部颜色和线条颜色。阴影使用线条颜色,因此如果两者相同,它将不可见。设置不同的线条颜色可以解决这个问题。

    import matplotlib.pyplot as plt
    
    teams = ["Brazil", "Uruguay", "Peru"]
    points = [6, 3, 1]
    
    bars = plt.bar(teams, points)
    
    bars[0].set_facecolor("r")
    bars[0].set_edgecolor("b")
    bars[0].set_hatch("/")
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-30
      • 2020-06-13
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      相关资源
      最近更新 更多