我有一个情节

(https://i.stack.imgur.com/OI4Bb.png)

基于matplotlib网站的示例:https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html

但我修改了它以适应我的数据:

import matplotlib.pyplot as plt
import numpy as np

def plot_A2():

    fysiker = ("JB", "EG")


    volymer_A2 = {
        'M4': (22.9, 17.6), 
        'M5': (22.0, 16.4),
        'M6': (21.4, 16.0),
        'M7': (21.8, 17.6),
    }

    x = np.arange(len(fysiker))  # the label locations
    width = 0.15  # the width of the bars
    multiplier = -0.5

    fig, ax = plt.subplots(layout='constrained')


    for attribute, measurement in volymer_A2.items():
        offset = width * multiplier
        rects = ax.bar(x + offset, measurement, width, label=attribute)
        ax.bar_label(rects, padding=3)
        multiplier += 1
  
    # Plottar sann volym
    volume_A2 = 18.77
    ax.plot([-2,5], [volume_A2, volume_A2], "k--", linewidth=1, color='gray', label='True volume')
   

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('Volume (ml)')
    ax.set_title('Phantom A2')
    ax.set_xticks(x + width, fysiker)
    ax.legend(loc='upper center')
    ax.set_ylim(15, 25)
    ax.set_xlim(-0.2, 1.5)
    #ax.grid()



    plt.show()

但是,我希望条的 colored颜色 是灰度级.如果可能的话,用一些模式来区分它们,比如这样的:

(https://i.stack.imgur.com/WGyin.png)

你会怎么做?

感谢你的帮助!

推荐答案

我们可以通过改变rects = ax.bar(x + offset, measurement, width, label=attribute)来获得这样的效果,我们可以把关键字Hatch 作为参数添加到其中,这将标准ASCII字符作为重复模式.

这是我改变的

    patterns = ['/', '.','+','o']  # Define the patterns for the bars

for index, (attribute, measurement) in enumerate(volymer_A2.items()):
    offset = width * multiplier
    rects = ax.bar(x + offset, measurement, width, color='gray', hatch=patterns[index%len(patterns)], label=attribute)  # Use hatch parameter to set the pattern
    ax.bar_label(rects, padding=3)
    multiplier += 1

而且我把这条线上的条的 colored颜色 设置为灰色.希望它能帮助

enter image description here

Python相关问答推荐

Class_weight参数不影响RandomForestClassifier不平衡数据集中的结果

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

如何使用Python将工作表从一个Excel工作簿复制粘贴到另一个工作簿?

如何让剧作家等待Python中出现特定cookie(然后返回它)?

Python 约束无法解决n皇后之谜

如何获取numpy数组的特定索引值?

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

如何在Polars中从列表中的所有 struct 中 Select 字段?

迭代嵌套字典的值

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

通过ManyToMany字段与Through在Django Admin中过滤

如何从列表框中 Select 而不出错?

比Pandas 更好的 Select

使用xlsxWriter在EXCEL中为数据帧的各行上色

Django更新视图未更新

Pandas 删除只有一种类型的值的行,重复或不重复

为什么在不先将包作为模块导入的情况下相对导入不起作用

我可以同时更改多个图像吗?

奇怪的Base64 Python解码

利用广播使减法更有效率