python – 在matplotlib patchcollection中设置颜色范围

栏目: Python · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/6028675/setting-color-range-in-matplotlib-patchcollection
我正在使用从文件中读入的coords和patch颜色值在matplotlib中绘制一个 PatchCollection

.

问题是matplotlib似乎会自动将颜色范围缩放到数据值的最小值/最大值.如何手动设置颜色范围?例如.如果我的数据范围是10-30,但我想将其缩放到5-50的颜色范围(例如与另一个图比较),我该怎么做?

我的绘图命令看起来与api示例代码中的相同: patch_collection.py

colors = 100 * pylab.rand(len(patches))
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
p.set_array(pylab.array(colors))
ax.add_collection(p)
pylab.colorbar(p)

pylab.show()
在示例中,使用p.set_clim([5,50])设置颜色缩放最小值和最大值. matplotlib中有颜色图的任何东西都有get_clim和 set_clim

方法.

作为一个完整的例子:

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle
import numpy as np

# (modified from one of the matplotlib gallery examples)
resolution = 50 # the number of vertices
N = 100
x       = np.random.random(N)
y       = np.random.random(N)
radii   = 0.1*np.random.random(N)
patches = []
for x1,y1,r in zip(x, y, radii):
    circle = Circle((x1,y1), r)
    patches.append(circle)

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

colors = 100*np.random.random(N)
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
p.set_array(colors)
ax.add_collection(p)
plt.colorbar(p)

plt.show()

现在,如果我们在调用plt.show(…)之前在某处添加p.set_clim([5,50])(其中p是补丁集合),我们得到:

翻译自:https://stackoverflow.com/questions/6028675/setting-color-range-in-matplotlib-patchcollection


以上所述就是小编给大家介绍的《python – 在matplotlib patchcollection中设置颜色范围》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具