拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 绘制许多彩色矩形

绘制许多彩色矩形

白鹭 - 2022-02-13 1988 0 0

我有要绘制的资料。我认为最好的方法是使用一系列矩形。我希望每个矩形跨越宽度 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()
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *