これはlinkのmatplotlibドキュメントからかなり良い例です。例から、以下の適応は、より柔軟性のある:次のプロットになり
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
def gbar(ax, x, y, width=0.5, bottom=0):
X = [[.6, .6], [.7, .7]]
for left, top in zip(x, y):
right = left + width
ax.imshow(X, interpolation='bicubic', cmap="copper",
extent=(left, right, bottom, top), alpha=1)
A = [5, 30, 45, 80]
x = [i + 0.5 for i in range(4)]
fig = figure()
xmin, xmax = xlim = 0.25, 4.5
ymin, ymax = ylim = 0, 100
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
autoscale_on=False)
gbar(ax, x, A, width=0.7)
ax.set_aspect('auto')
plt.show()
、::
![Bar plot with gradients on the bars in matplotlib](https://i.stack.imgur.com/IXHZu.png)
EDITあなたの例にこれを適応しますが、このような何かを得るでしょうimshowと協力してこの中
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
def gbar(ax, x, y, width=0.5, bottom=0):
X = np.arange(100)[:, np.newaxis]
for left, top in zip(x, y):
right = left + width
mask = X > top
ax.imshow(np.ma.masked_array(X, mask), origin="lower", interpolation='nearest', cmap="RdYlGn", vmin=0, vmax=100,
extent=(left, right, bottom, 100), alpha=1)
A = [5, 30, 45, 80]
x = [i + 0.5 for i in range(4)]
fig = figure()
xmin, xmax = xlim = 0.25, 4.5
ymin, ymax = ylim = 0, 100
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
autoscale_on=False)
gbar(ax, x, A, width=0.7)
ax.set_aspect('auto')
ax.set_yticks([i for i in range(0, 100, 10)])
ax.set_yticklabels([str(i) + " %" for i in range(0, 100, 10)])
plt.show()
、結果:例よう
![Another implementation of imshow as a barplot](https://i.stack.imgur.com/aPiY4.png)
あなたは私が厳密に増加単調100個の要素の配列(建物だということに気づくでしょう:ステップ1で100に0を)。
X = np.arange(100)[:, np.newaxis]
そして、それぞれのバーの上部の上にすべてをマスキング:
mask = X > top
np.ma.masked_array(X, mask)
はまた、私は、それぞれ、0と100などvmin
とvmax
を記載しています。これにより、常に100の位置に緑色が表示され、0の位置に赤色が表示されます。これがあなたが探している効果であればそのまま使用できます。
バーでより多くのカスタマイズを実現する最も簡単な方法は、X内の値を再生することです(カラーマップされているため)。しかし、明らかにそれのために何らかのルールが必要です。あなたの例では、オレンジは時々40%、他の30%と20%(それはバーのサイズに関連しているようですが、私はその関係を知らない)で現れます。バーの1つは完全に緑色であり、これはもう一つのルールです。
コード内で例外を作成する必要があります。たとえば、上端が90%を超えるときは、緑色のカラーマップを使用します(または任意のもの)。
5番、30番、45番、80番は何も意味しないか、それとも棒の高さですか?あなたはすべてのバーが赤から始まり、緑で終わるか、またはすべてが赤から始まりますが、異なる色で終わるのは高さによって決まりますか? – Chu
こんにちは、あなたのリプレイに感謝します。私は図のようなものが欲しい。だから、例えば、5のときは赤だけ、30の赤とオレンジ、...、100の尺度が欲しいとき。 – Smartis