gpt4 book ai didi

python - 如何用渐变填充 matplotlib 条形图?

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:45 25 4
gpt4 key购买 nike

我非常有兴趣用不同的梯度填充 barplot 的 matplotlib/seaborn 条,就像这里所做的那样(据我所知,不是用 matplotlib): enter image description here

我也检查了这个相关主题Pyplot: vertical gradient fill under curve? .

这是否只能通过 gr-framework 实现: enter image description here还是有替代策略?

最佳答案

正如Pyplot: vertical gradient fill under curve?中描述的那样人们可以使用图像来创建梯度图。

由于条形图是矩形的,因此图像的范围可以直接设置为条形图的位置和大小。可以遍历所有条形并在相应位置创建图像。结果是一个梯度条形图。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

bar = ax.bar([1,2,3,4,5,6],[4,5,6,3,7,5])

def gradientbars(bars):
grad = np.atleast_2d(np.linspace(0,1,256)).T
ax = bars[0].axes
lim = ax.get_xlim()+ax.get_ylim()
for bar in bars:
bar.set_zorder(1)
bar.set_facecolor("none")
x,y = bar.get_xy()
w, h = bar.get_width(), bar.get_height()
ax.imshow(grad, extent=[x,x+w,y,y+h], aspect="auto", zorder=0)
ax.axis(lim)

gradientbars(bar)

plt.show()

enter image description here

关于python - 如何用渐变填充 matplotlib 条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830250/

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