27

たとえば、matplotlib で色を Rectangle に設定するにはどうすればよいですか? 引数の色を使用しようとしましたが、成功しませんでした。

私は次のコードを持っています:

fig=pylab.figure()
ax=fig.add_subplot(111)

pylab.xlim([-400, 400])    
pylab.ylim([-400, 400])
patches = []
polygon = Rectangle((-400, -400), 10, 10, color='y')
patches.append(polygon)

p = PatchCollection(patches, cmap=matplotlib.cm.jet)
ax.add_collection(p)
ax.xaxis.set_major_locator(MultipleLocator(20))    
ax.yaxis.set_major_locator(MultipleLocator(20))    

pylab.show()
4

3 に答える 3

43

コードを機能させることができませんでしたが、これがお役に立てば幸いです。

import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
rect1 = matplotlib.patches.Rectangle((-200,-100), 400, 200, color='yellow')
rect2 = matplotlib.patches.Rectangle((0,150), 300, 20, color='red')
rect3 = matplotlib.patches.Rectangle((-300,-50), 40, 200, color='#0099FF')
circle1 = matplotlib.patches.Circle((-200,-250), radius=90, color='#EB70AA')
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)
ax.add_patch(circle1)
plt.xlim([-400, 400])
plt.ylim([-400, 400])
plt.show()

生成: ここに画像の説明を入力してください

于 2012-05-11T12:33:55.293 に答える
6

ax.add_artist(Rectangle)色の仕様を機能させるには、次の作業を行う必要があります。を使用するpatches.append(Rectangle)と、長方形は(少なくとも私のPCでは)色の指定を無視して青色で表示されます。

ところで、アーティストに注意してください— Matplotlib 1.2.1 ドキュメント: class matplotlib.patches.Rectangleには、

  • edgecolor- ストロークの色
  • facecolor- 塗りつぶしの色

...そして、color基本的にストロークと塗りつぶしの両方の色を同時に設定する - があります。

これは、Linux (Ubuntu 11.04)、python 2.7、matplotlib 0.99.3 でテストした、変更された OP コードです。

import matplotlib.pyplot as plt
import matplotlib.collections as collections
import matplotlib.ticker as ticker

import matplotlib
print matplotlib.__version__ # 0.99.3

fig=plt.figure() #pylab.figure()
ax=fig.add_subplot(111)

ax.set_xlim([-400, -380]) #pylab.xlim([-400, 400])
ax.set_ylim([-400, -380]) #pylab.ylim([-400, 400])
patches = []
polygon = plt.Rectangle((-400, -400), 10, 10, color='yellow') #Rectangle((-400, -400), 10, 10, color='y')
patches.append(polygon)

pol2 = plt.Rectangle((-390, -390), 10, 10, facecolor='yellow', edgecolor='violet', linewidth=2.0)
ax.add_artist(pol2)


p = collections.PatchCollection(patches) #, cmap=matplotlib.cm.jet)
ax.add_collection(p)
ax.xaxis.set_major_locator(ticker.MultipleLocator(20)) # (MultipleLocator(20)) 
ax.yaxis.set_major_locator(ticker.MultipleLocator(20)) # (MultipleLocator(20)) 

plt.show() #pylab.show()

これは出力です:

matplotlib.png

于 2013-03-26T17:39:18.650 に答える