繁体   English   中英

在 matplotlib patchcollect 中设置颜色范围

[英]setting color range in matplotlib patchcollection

我正在使用从文件中读取的坐标和补丁颜色值在 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])使用p.set_clim([5, 50])设置颜色缩放的最小值和最大值。 matplotlib 中任何有颜色图的东西都有get_climset_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)
fig.colorbar(p)

fig.show()

在此处输入图片说明

现在,如果我们在调用fig.show(...)之前在某处添加p.set_clim([5, 50]) (其中p是补丁集合fig.show(...) ,我们会得到:在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM