Matplotlib条形图的渐变填充颜色

5
我该如何在我的条形图中添加渐变facecolor?我想知道如何使用单色渐变到复杂的多色渐变。以下是一些示例代码:
import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(1,5))
plt.axis([0,1,-50,200])
plt.xticks([])
plt.yticks([-40,180])
plt.bar(left=0, width=1, bottom=-40, height=220, color='r')
plt.subplots_adjust(left=0.4, right=0.8)
plt.savefig("test.png")
1个回答

7
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# http://matplotlib.sourceforge.net/examples/api/colorbar_only.html

fig=plt.figure(figsize=(1,5))
ax=fig.add_subplot(111)
ax.axis([0,1,-50,200])
cmap = mpl.cm.jet
norm = mpl.colors.Normalize(vmin=-40, vmax=180)
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
                                orientation='vertical',                                
                                norm=norm,
                                ticks=[-40,180]
                                )

plt.subplots_adjust(left=0.4, right=0.8)
plt.show()

enter image description here


3
这似乎不是一个面色?就像“matplotlib.axes._base”模块中的“set_facecolor”函数,我期望(从谷歌搜索中得知)有一些设置mpl面色的示例。鉴于提问者接受了这个答案,我不确定他们是否想要这个(也许他们对我来说想要不同的东西)。 - baxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接