[英]setting color range in matplotlib patchcollection
I am plotting a PatchCollection
in matplotlib with coords and patch color values read in from a file.我正在使用从文件中读取的坐标和补丁颜色值在 matplotlib 中绘制
PatchCollection
。
The problem is that matplotlib seems to automatically scale the color range to the min/max of the data values.问题是 matplotlib 似乎自动将颜色范围缩放到数据值的最小值/最大值。 How can I manually set the color range?
如何手动设置颜色范围? Eg if my data range is 10-30, but I want to scale this to a color range of 5-50 (eg to compare to another plot), how can I do this?
例如,如果我的数据范围是 10-30,但我想将其缩放到 5-50 的颜色范围(例如与另一个图进行比较),我该怎么做?
My plotting commands look much the same as in the api example code: patch_collection.py我的绘图命令与 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()
Use p.set_clim([5, 50])
to set the color scaling minimums and maximums in the case of your example.在您的示例中
p.set_clim([5, 50])
使用p.set_clim([5, 50])
设置颜色缩放的最小值和最大值。 Anything in matplotlib that has a colormap has the get_clim
and set_clim
methods. matplotlib 中任何有颜色图的东西都有
get_clim
和set_clim
方法。
As a full example:作为一个完整的例子:
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)
fig.colorbar(p)
fig.show()
Now, if we just add p.set_clim([5, 50])
(where p
is the patch collection) somewhere before we call fig.show(...)
, we get this:现在,如果我们在调用
fig.show(...)
之前在某处添加p.set_clim([5, 50])
(其中p
是补丁集合fig.show(...)
,我们会得到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.