擅长:python、mysql、java
<p>正如<a href="https://stackoverflow.com/questions/22081361/pyplot-vertical-gradient-fill-under-curve">Pyplot: vertical gradient fill under curve?</a>中所描述的,可以使用图像来创建渐变图。在</p>
<p>由于条形图是矩形的,所以图像的范围可以直接设置为条形图的位置和大小。你可以循环所有的酒吧和创建一个图像在各自的位置。结果是一个渐变条形图。在</p>
<pre><code>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()
</code></pre>
<p><a href="https://i.stack.imgur.com/diuwV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/diuwV.png" alt="enter image description here"/></a></p>