简体   繁体   English

我可以从 colors 和 for 循环的列表中 plot 一个颜色条吗?

[英]Can I plot a colorbar from a list of colors and for loop?

Here's part of the Python code I'm working on:这是我正在处理的 Python 代码的一部分:

cm = LinearSegmentedColormap.from_list('defcol', ["#000000", "#FF0000"])
trace_color = cm(np.linspace(0,1,cycles))
for k, color in zip(range(cycles),trace_color):
        lis = KL_rest[k::cycles]
        plt.scatter(scanpoints, lis, color = color, marker = '^', alpha = 0.9)

Here I am generating the scatter plot using a for loop, and the colors come from the list trace_color .在这里,我使用 for 循环生成散点图 plot,而 colors 来自列表trace_color My question is if I can generate a colorbar, where the colors are from the trace_color and labels (scale) on the colorbar come from range(cycles) .我的问题是我是否可以生成一个颜色条,其中 colors 来自trace_color并且颜色条上的标签(比例)来自range(cycles) I tried to add plt.colorbar() after the for loop but that didn't work.我试图在 for 循环之后添加plt.colorbar()但这没有用。 Thanks!!谢谢!!

Matplotlib's colorbar needs a ScalarMappable object. By default, it is taken from what's plotted, eg a scatter plot that is created in one call. Matplotlib 的颜色条需要一个ScalarMappable object。默认情况下,它取自绘制的内容,例如在一次调用中创建的散点图 plot。 If you need to combine the results of multiple calls, you can create an own ScalarMappable .如果您需要组合多个调用的结果,您可以创建一个自己的ScalarMappable It needs a colormap and a norm .它需要一个 colormap 和一个 norm

from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.cm import ScalarMappable
import numpy as np

cycles = 7
N = 100
KL_rest = np.sin(np.linspace(0, 10 * np.pi, cycles * N))
scanpoints = np.arange(N)
cm = LinearSegmentedColormap.from_list('defcol', ["#000000", "#FF0000"])
trace_color = cm(np.linspace(0, 1, cycles))
for k, color in zip(range(cycles), trace_color):
    lis = KL_rest[k::cycles]
    plt.scatter(scanpoints, lis, color=color, marker='^', alpha=0.9)
plt.colorbar(ScalarMappable(cmap=cm, norm=plt.Normalize(0, cycles - 1)), ticks=np.arange(cycles), label='cycles')
plt.show()

自定义颜色条

Note that in this case, you can create the scatter plot in one go, enabling a default color bar.请注意,在这种情况下,您可以在一个 go 中创建散点图 plot,启用默认颜色条。 For this to work, the scanpoints can be repeated for each cycle, and the colors can be indicated by tiling the cycle for each scan point.为此,可以为每个循环重复扫描点,并且可以通过为每个扫描点平铺循环来指示scanpoints

If you only want to show the really used colors, you can add N=cycles in the creation of the color map. To put the tick marks for each number in the center of the cells, you can move the default limits by 0.5 .如果只想显示真正使用过的 colors,可以在颜色 map 的创建中添加N=cycles 。要将每个数字的刻度线放在单元格的中心,可以将默认限制移动0.5

from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

cycles = 7
N = 100
KL_rest = np.sin(np.linspace(0, 10 * np.pi, cycles * N))
scanpoints = np.arange(N)
cm = LinearSegmentedColormap.from_list('defcol', ["#000000", "#FF0000"], N=cycles)

plt.scatter(np.repeat(scanpoints, cycles), KL_rest,
            c=np.tile(range(cycles), len(scanpoints)),
            cmap=cm, norm=plt.Normalize(-0.5, cycles - 0.5),
            marker='^', alpha=0.9)
plt.colorbar(ticks=np.arange(cycles), label='cycles')
plt.show()

一次性绘制散点图

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

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