我有要繪制的資料。我認為最好的方法是使用一系列矩形。我希望每個矩形跨越寬度 delta_t(每個時間間隔相同)和高度 delta_f(頻率間隔可能不同),并且每個矩形的顏色由 log(z) 給出。
編輯:下面的奇妙答案解決了我的大部分繪圖問題。我用回圈轉身,但我很快就解決了。我感謝您的幫助。
uj5u.com熱心網友回復:
為了獲得這個,我添加或更改了一些東西:

將 y 軸刻度更改為對數。
手動更改 x 和 y 限制,以便矩形適合。
norm為您指定 aPatchCollection以便它知道如何將值轉換為顏色。沒有這個,你只能使用你不想要的 0-1 范圍。指定
array你的PatchCollection,以便它知道將哪些值變成顏色。zex[i][j]為此,我們存盤提供的值串列。無需提供這些值make_rectangle(無論如何它們都沒有使用)。
理論上,您可以從資料中自動計算范數的最小值和最大值以及軸的極限。在這里,我采用了您在 OP (1e-6, 10) 和手動限制中給出的規范。
# Imports.
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle
from matplotlib.colors import LogNorm
import numpy as np
def make_rectangle(t_min, f_min, delta_t, delta_f):
return Rectangle(xy = (t_min, f_min), width = delta_t, height = delta_f, edgecolor = 'k')
timeex = [0, 1, 2, 3, 4]
frequencyex = [0, 0.1, 1, 10, 100]
zex = [[1, 1.2, 1.1, 1.5, 1.6],
[0.01, 120, 0.11, 1.6, 1.5],
[0.1, 0.12, 1.1e-6, 15, 16],
[1, 1.2, 1.1, 1.5, 1.6],
[0.01, 120, 0.11, 1.6, 1.5]]
tiles = []
values = []
for i in range(len(timeex) - 1):
t_min = timeex[i]
f_min = frequencyex[i]
t_max = timeex[i 1]
f_max = frequencyex[i 1]
for j in range(len(zex[i])):
rect = make_rectangle(t_min, f_min, t_max - t_min, f_max - f_min)
tiles.append(rect)
values.append(zex[i][j])
# Create figure and ax.
fig = plt.figure()
ax = fig.add_subplot(111)
# Normalize entry values to 0-1 for the colormap, and add the colorbar.
norm = LogNorm(vmin=1e-6, vmax=10)
p = PatchCollection(tiles, cmap=plt.cm.jet, norm=norm, match_original=True) # You need `match_original=True` otherwise you lose the black edgecolor.
fig.colorbar(p)
ax.add_collection(p)
# Set the "array" of the patch collection which is in turn used to give the appropriate colors.
p.set_array(np.array(values))
# Scale the axis so that the rectangles show properly. This can be done automatically
# from the data of the patches but I leave this to you.
ax.set_yscale("log")
ax.set_xlim(0, 8)
ax.set_ylim(0, 100)
fig.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415795.html
標籤:
上一篇:Python:重建玫瑰圖
