gpt4 book ai didi

python - 自定义多色水平条形图 matplotlib

转载 作者:行者123 更新时间:2023-12-02 01:59:27 25 4
gpt4 key购买 nike

我想创建一个水平条形图,其中每个条形都有自定义的条纹配色方案(类似于下图)。我熟悉创建普通的 barhplot,但我不知道双色方案如何工作。

enter image description here

尝试过的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({"P1_P2": [0.20],
"P1_P3": [0.16],
"P2_P5": [0.12],
"P3_P5": [0.06],
"P3_P6": [0.06],
"P5_P6": [0.06]})
df=df.T

fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black')

df.plot.barh(ax=ax, position=0.50, width=0.3, color=(245/255, 153/255, 145/255, 1.0))
ax.get_legend().remove()

plt.show()

最佳答案

您可以用不同颜色填充每个条形。线条的宽度需要根据特定的图进行调整。

由于轮廓线使用相同的颜色,因此可以创建仅显示轮廓线的条形副本。

import matplotlib.pyplot as plt
from matplotlib import rcParams
import pandas as pd
from copy import copy

df = pd.DataFrame([0.2 , 0.16, 0.12, 0.06, 0.06, 0.06], index=['P1_P2', 'P1_P3', 'P2_P5', 'P3_P5', 'P3_P6', 'P5_P6'])
first_colors = ['dodgerblue', 'orange', 'green','silver', 'silver', 'dodgerblue']
second_colors = ['crimson', 'crimson', 'dodgerblue', 'dodgerblue', 'green', 'gold']

fig, ax = plt.subplots(dpi=600, figsize=(4, 4), nrows=1, ncols=1, facecolor=None, edgecolor='black')

df.plot.barh(ax=ax, position=0.50, width=0.3, legend=False, linewidth=0)

rcParams['hatch.linewidth'] = 4
for bar, main_color, hatch_color in zip(ax.containers[0], first_colors, second_colors):
rect = copy(bar)
rect.set_facecolor('none')
rect.set_edgecolor('black')
rect.set_linewidth(2)
ax.add_patch(rect)
bar.set_facecolor(main_color)
bar.set_edgecolor(hatch_color)
bar.set_hatch('//')
ax.invert_yaxis()
plt.tight_layout()
plt.show()

multi-colored hatched bars

PS:获取轮廓的更简单版本可能是绘制两次条形的轮廓版本,然后更新首先绘制的条形的颜色和阴影线(第一个条形存储在 ax .containers[0]).

for _ in range(2):
df.plot.barh(ax=ax, position=0.50, width=0.3, legend=False, fc='none', ec='black', lw=2)
rcParams['hatch.linewidth'] = 4
for bar, main_color, hatch_color in zip(ax.containers[0], first_colors, second_colors):
bar.set_linewidth(0)
bar.set_facecolor(main_color)
bar.set_edgecolor(hatch_color)
bar.set_hatch('//')

关于python - 自定义多色水平条形图 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69165744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com