python - 如何在 Matplotlib 中将颜色设置为矩形?

标签 python 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()

产生: enter image description here

关于python - 如何在 Matplotlib 中将颜色设置为矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10550477/

相关文章:

python-3.x - 如何使用 matplotlib.pyplot 基于 2D 中的 3 个点(x,y)绘制三角形?

python - matplotlib bwr-colormap,始终以零为中心

python - 具有自定义渐变着色的单堆积条形图

python - 如何使 readline 在 python 子进程中工作?

python - Selenium 找不到元素

python - 将 UTF-8 值打印为原始字符而不是代码

python - 如何在 python 中绘制 4 维数据的 kmeans 聚类?

python - 直方图中的 Matplotlib PyPlot 线

python - Django 模型中的类属性继承

python - 为什么 python SqlAlchemy Boolean 和 Integer Type 之间的插入性能差异很大