如何用渐变填充matplotlib条? [英] How to fill matplotlib bars with a gradient?

查看:116
本文介绍了如何用渐变填充matplotlib条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将非常有兴趣以完全像这里所做的那样用不同的渐变填充barplot的matplotlib/seaborn条(据我所知,不是用matplotlib填充):

I would be very interested in filling matplotlib/seaborn bars of a barplot with different gradients exactly like done here (not with matplotlib as far as I understood):

我还检查了以下相关主题 Pyplot:曲线下方的垂直渐变填充?.

I have also checked this related topic Pyplot: vertical gradient fill under curve?.

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

Is this only possible via gr-framework: or are there alternative strategies?

推荐答案

就像在 Pyplot:曲线下的垂直渐变填充?可能会使用一幅图像来创建渐变图.

Just as depicted in Pyplot: vertical gradient fill under curve? one may use an image to create a gradient plot.

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

Since bars are rectangular the extent of the image can be directly set to the bar's position and size. One can loop over all bars and create an image at the respective position. The result is a gradient bar plot.

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() 

这篇关于如何用渐变填充matplotlib条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆