[英]Why is matplotlib.PatchCollection messing with color of the patches?
[英]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_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)
fig.colorbar(p)
fig.show()
現在,如果我們在調用fig.show(...)
之前在某處添加p.set_clim([5, 50])
(其中p
是補丁集合fig.show(...)
,我們會得到:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.