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

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

内容简介:翻译自: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中设置颜色范围》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Head First Design Patterns

Head First Design Patterns

Elisabeth Freeman、Eric Freeman、Bert Bates、Kathy Sierra、Elisabeth Robson / O'Reilly Media / 2004-11-1 / USD 49.99

You're not alone. At any given moment, somewhere in the world someone struggles with the same software design problems you have. You know you don't want to reinvent the wheel (or worse, a flat tire),......一起来看看 《Head First Design Patterns》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具