简体   繁体   English

使用colormap绘制数据,在我的matplotlib绘图旁边创建一个颜色条

[英]Creating a colorbar next to my matplotlib plot w/o using the colormap to plot the data

I'm having trouble creating a colorbar for my plot in Python using matplotlib. 我在使用matplotlib在Python中为我的绘图创建颜色条时遇到了麻烦。 I am using a colormap, not to colour all the data that I plot but to extract a colour for a plot based on a value I'm not plotting. 我正在使用色彩图,不是为我绘制的所有数据着色,而是根据我没有绘制的值为图绘制颜色。 Hope this makes sense.. So I'm in a for loop, create a plot every time with a colour based on a certain parameter. 希望这是有道理的。所以我在for循环中,每次使用基于某个参数的颜色创建一个绘图。 Like this (the data is an example to create an mwe, my data is more complicated): 像这样(数据是创建一个mwe的例子,我的数据更复杂):

import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np

xdata = np.array(range(10))
parameter = [0.5, 0.3, 0.78, 0.21, 0.45] #random parameter example

cmap = mpl.cm.get_cmap('jet')

for i in range(len(parameter)):
    clr = cmap(parameter(i))
    plt.plot(xdata,xdata**i,c=clr)

plt.show()

Now, what I would want is a colorbar on the side (or actually two, but that's another problem I think) that shows the jet colormap and according values. 现在,我想要的是侧面的颜色条(或实际上是两个,但我认为这是另一个问题),它显示了喷射色图和相应的值。 The values need to be scaled to a new min and max value. 需要将值缩放到新的最小值和最大值。

So far I've found the following, but I don't understand it enough to apply it to my own problem: 到目前为止,我已经找到了以下内容,但我不太了解将其应用于我自己的问题:

Getting individual colors from a color map in matplotlib Which told me how to extract the colour and shows how to create the normalized colormap 从matplotlib中的颜色贴图中获取单独的颜色告诉我如何提取颜色并演示如何创建规范化的颜色图

Colorbar only Which should tell me how to add a colorbar without using the plotted data, but I don't understand enough of it. 仅限Colorbar哪个应该告诉我如何在不使用绘制数据的情况下添加颜色条 ,但我对它不够了解。 My problem is with the creation of the axes. 我的问题是轴的创建。 I don't understand this part if I want to put the colorbar next to my plot. 如果我想把颜色条放在我的情节旁边,我不明白这一部分。 In the example they create a figure with handle fig , but in my case the figure is created when I do plt.imshow(image) , since this is what I start with and then I'm plotting over the image. 在示例中,他们创建了一个带有句柄fig的图形,但在我的情况下,当我执行plt.imshow(image)时会创建plt.imshow(image) ,因为这是我开始的,然后我正在绘制图像。 I cannot use the fig.add_axes here. 我不能在这里使用fig.add_axes

I hope you can help me out here. 我希望你能在这里帮助我。 It would be great if I could also create a 'reversed' colorbar. 如果我还可以创建一个“反转”颜色条,那将是很棒的。 So either the colours are in reverse direction, or the values next to the bar. 因此颜色是反方向,或者是条形旁边的值。

At any point in the script you can get the figure via fig = plt.gcf() and an axes via ax=plt.gca() . 在脚本中的任何位置,您都可以通过fig = plt.gcf()获得图形,通过ax=plt.gca()获得轴。 So, adding an axes may be done by plt.gcf().add_axes(...) . 因此,添加轴可以通过plt.gcf().add_axes(...)
There is also nothing wrong with putting fig=plt.figure() before plotting anything. 在绘制任何东西之前放置fig=plt.figure()也没什么不对。

Note that after creating a new axes, plt.gca() will return the new axes, so it is a good idea to create a reference to the main axes before adding a new one. 请注意,创建新轴后, plt.gca()将返回新轴,因此在添加新轴之前创建对主轴的引用是个好主意。
A convenient way to obtain a figure and an axes for later referencing is to create the figure via 获取图形和轴以供以后参考的便捷方法是通过创建图形

fig, ax = plt.subplots()

Colormaps: Every standard colormap has a reversed version, which has _r at the end of its name, eg you can use viridis_r instead of viridis . 色彩映射每个标准色彩映射都有一个反转版本,其名称末尾有_r ,例如,您可以使用viridis_r而不是viridis

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

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