gpt4 book ai didi

python + matplotlib : how can I change the bar's line width for a single bar?

转载 作者:太空狗 更新时间:2023-10-29 21:26:45 24 4
gpt4 key购买 nike

我有一个包含 3 个堆叠系列和 5 个条形图的条形图。我想通过更改线条的宽度来突出显示一个条(所有 3 个堆叠元素)。

我正在使用以下命令绘制条形图:

mybar = ax.bar(x,Y[:,i],bottom=x,color=colors[i],edgecolor='none',width=wi,linewidth = 0)
bar_handles = np.append(bar_handles,mybar)

我有存储在数组 bar_handles 中的要更改的条形句柄,有没有办法更改条形图的 edgecolorlinewidth 绘制后的属性?

最佳答案

ax.bar 返回艺术家的 Container;每个“艺术家”都是一个具有 set_linewidthset_edgecolor 方法的 Rectangle

要更改设置,比如 mybar 中的第二个栏,您可以这样做:

mybar[1].set_linewidth(4)
mybar[1].set_edgecolor('r')

这是一个脚本,展示了如何使用它来更改堆栈的线宽:

import numpy as np
import matplotlib.pyplot as plt


x = np.array([1,2,3])
y1 = np.array([3,2.5,1])
y2 = np.array([4,3,2])
y3 = np.array([1,4,1])

width = 0.5
handles = []
b1 = plt.bar(x, y1, color='#2040D0', width=width, linewidth=0)
handles.append(b1)
b2 = plt.bar(x, y2, bottom=y1, color='#60A0D0', width=width, linewidth=0)
handles.append(b2)
b3 = plt.bar(x, y3, bottom=y1+y2, color='#A0D0D0', width=width, linewidth=0)
handles.append(b3)

# Highlight the middle stack.
for b in handles:
b[1].set_linewidth(3)

plt.xlim(x[0]-0.5*width, x[-1]+1.5*width)
plt.xticks(x+0.5*width, ['A', 'B', 'C'])
plt.show()

此脚本创建以下条形图:

stacked bar chart

关于 python + matplotlib : how can I change the bar's line width for a single bar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125199/

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