Mar*_*k W 8 python patch matplotlib
我PatchCollection
在matplotlib中绘制一个带有coords和补丁颜色值的文件.
问题是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()
Run Code Online (Sandbox Code Playgroud)
Joe*_*ton 22
使用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()
Run Code Online (Sandbox Code Playgroud)
现在,如果我们在调用之前在某处添加p.set_clim([5, 50])
(p
补丁集合在哪里)plt.show(...)
,我们得到:
归档时间: |
|
查看次数: |
13363 次 |
最近记录: |