[英]How do I set color to Rectangle in Matplotlib?
例如在 matplotlib 中如何将颜色设置为矩形? 我尝试使用参数颜色,但没有成功。
我有以下代码:
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()
我无法让你的代码工作,但希望这会有所帮助:
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()
生产:
事实证明,你需要做ax.add_artist(Rectangle)
才能使颜色规格有效; 当使用patches.append(Rectangle)
,矩形显示为蓝色(至少在我的PC上),忽略任何颜色规范。
顺便说一句,请注意艺术家 - Matplotlib 1.2.1文档:类matplotlib.patches.Rectangle说明有
edgecolor
- 用于描边颜色 facecolor
- 用于填充颜色 ...然后有color
- 它基本上同时设置笔触和填充颜色。
这是修改后的OP代码,我在Linux(Ubuntu 11.04),python 2.7,matplotlib 0.99.3上测试过:
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()
这是输出:
为避免多次调用.add_patch()
(通常是首先使用PatchCollection的目的),您可以通过cmap=
将PatchCollection
传递给ListedColormap
。
这看起来如下(根据fraxel的答案修改):
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.collections import PatchCollection
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
patches_list = []
color_list = []
patches_list.append(matplotlib.patches.Rectangle((-200,-100), 400, 200))
color_list.append('yellow')
patches_list.append(matplotlib.patches.Rectangle((0,150), 300, 20))
color_list.append('red')
patches_list.append(matplotlib.patches.Rectangle((-300,-50), 40, 200))
color_list.append('#0099FF')
patches_list.append(matplotlib.patches.Circle((-200,-250), radius=90))
color_list.append('#EB70AA')
our_cmap = ListedColormap(color_list)
patches_collection = PatchCollection(patches_list, cmap=our_cmap)
patches_collection.set_array(np.arange(len(patches_list)))
ax.add_collection(patches_collection)
plt.xlim([-400, 400])
plt.ylim([-400, 400])
plt.show()
我遇到了同样的问题。 只有在创建PatchCollection()
时指定match_original=True
时,将color=
传递给Rectangle()
才会起作用。 有关详细信息,请参阅PatchCollection() 。
@Didillysquat 示例的简化版本是
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
fig = plt.figure()
ax = fig.add_subplot(111)
patches_list = []
color_list = []
patches_list.append(matplotlib.patches.Rectangle((-200,-100), 400, 200, color='yellow'))
patches_list.append(matplotlib.patches.Rectangle((0,150), 300, 20, color='red'))
patches_list.append(matplotlib.patches.Rectangle((-300,-50), 40, 200, color='#0099FF'))
patches_list.append(matplotlib.patches.Circle((-200,-250), radius=90, color='#EB70AA'))
# Make sure you use match_original=True
patches_collection = PatchCollection(patches_list, match_original=True)
ax.add_collection(patches_collection)
plt.xlim([-400, 400])
plt.ylim([-400, 400])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.