设置颜色条范围
我有以下代码:
import matplotlib.pyplot as plt
cdict = {
'red' : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
'green': ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
'blue' : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}
cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)
plt.clf()
plt.pcolor(X, Y, v, cmap=cm)
plt.loglog()
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
plt.show()
这会使用指定的颜色图生成 X 轴与 Y 轴上的值 v
的图表。 X 轴和 Y 轴是完美的,但颜色图在 v
的最小值和最大值之间分布。我想强制颜色图的范围在 0 和 1 之间。
我想到使用:
plt.axis(...)
设置轴的范围,但这仅需要 X 和 Y 的最小值和最大值的参数,而不是颜色图。
编辑:
为了清楚起见,假设我有一个图的值范围为 (0 ... 0.3),另一个图的值范围为 (0.2 ... 0.8)。
在这两个图中,我希望颜色条的范围为 (0 ... 1)。在这两个图中,我希望使用上面的整个 cdict 范围使该颜色范围相同(因此两个图中的 0.25 将是相同的颜色)。在第一个图表中,0.3 到 1.0 之间的所有颜色都不会出现在图表中,但会出现在侧面的颜色条键中。在另一种情况下,0 到 0.2 之间以及 0.8 到 1 之间的所有颜色都不会出现在图表中,但会出现在侧面的颜色栏中。
I have the following code:
import matplotlib.pyplot as plt
cdict = {
'red' : ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
'green': ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
'blue' : ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}
cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)
plt.clf()
plt.pcolor(X, Y, v, cmap=cm)
plt.loglog()
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
plt.show()
This produces a graph of the values v
on the axes X vs Y, using the specified colormap. The X and Y axes are perfect, but the colormap spreads between the min and max of v
. I would like to force the colormap to range between 0 and 1.
I thought of using:
plt.axis(...)
To set the ranges of the axes, but this only takes arguments for the min and max of X and Y, not the colormap.
Edit:
For clarity, let's say I have one graph whose values range (0 ... 0.3), and another graph whose values (0.2 ... 0.8).
In both graphs, I will want the range of the colorbar to be (0 ... 1). In both graphs, I want this range of colour to be identical using the full range of cdict
above (so 0.25 in both graphs will be the same colour). In the first graph, all colours between 0.3 and 1.0 won't feature in the graph, but will in the colourbar key at the side. In the other, all colours between 0 and 0.2, and between 0.8 and 1 will not feature in the graph, but will in the colourbar at the side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
vmin
和vmax
强制颜色范围。这是一个示例:Using
vmin
andvmax
forces the range for the colors. Here's an example:使用 CLIM 函数(相当于 CAXIS 函数):
Use the CLIM function (equivalent to CAXIS function in MATLAB):
不确定这是否是最优雅的解决方案(这就是我使用的),但您可以将数据缩放到 0 到 1 之间的范围,然后修改颜色条:
通过两个不同的限制,您可以控制数据的范围和图例颜色条。在此示例中,栏中仅显示 -0.5 到 1.5 之间的范围,而颜色图涵盖 -2 到 2(因此这可能是您在缩放之前记录的数据范围)。
因此,您无需缩放颜色图,而是缩放数据并使颜色条适合该数据。
Not sure if this is the most elegant solution (this is what I used), but you could scale your data to the range between 0 to 1 and then modify the colorbar:
With the two different limits you can control the range and legend of the colorbar. In this example only the range between -0.5 to 1.5 is show in the bar, while the colormap covers -2 to 2 (so this could be your data range, which you record before the scaling).
So instead of scaling the colormap you scale your data and fit the colorbar to that.
如果您有多个绘图,使用图形环境和 .set_clim()
这种替代方案可能会更容易、更安全:
单个颜色条
最好的替代方案是对整个绘图使用单个颜色条。有不同的方法可以做到这一点,此教程对于理解最好的选择。我更喜欢这个解决方案,您可以简单地复制和粘贴,而不是前面的代码的可视化颜色栏部分。
PS
我建议使用
pcolormesh
而不是pcolor
因为它更快(更多此处的信息 )。Using figure environment and .set_clim()
Could be easier and safer this alternative if you have multiple plots:
A single colorbar
The best alternative is then to use a single color bar for the entire plot. There are different ways to do that, this tutorial is very useful for understanding the best option. I prefer this solution that you can simply copy and paste instead of the previous visualizing colorbar part of the code.
P.S.
I would suggest using
pcolormesh
instead ofpcolor
because it is faster (more infos here ).也可以通过在
pcolormesh
/pcolor
调用中将元组传递给clim=
kwarg 来设置颜色条范围。如果必须在
pcolormesh
调用后更新颜色条范围,那么最简单的方法是plt.clim
正如其他人提到的。另一种方法是通过由pcolormesh
/< 创建的QuadMesh
/PolyCollection
对象的colors.Normalize
对象代码>pcolor 调用。顺便说一句,
Quadmesh
驻留在 Axes 实例的集合
中,因此,如果pcolormesh
的结果未分配给变量,pmesh = plt.gca().collections[0]
也可以使用。使用tom10的设置,这个可以这样写。
在图的右侧附加一个颜色条可能会“更漂亮”,而不是为多个子图使用重复的颜色条。 @GM 已经提出了一种解决方案,通过对颜色条边界进行硬编码来实现这一点,但可以使用此处定义的函数动态设置边界。一个>。
对于手头的例子,可以写如下。
The colorbar range can be set by passing a tuple to
clim=
kwarg in thepcolormesh
/pcolor
call as well.If the colorbar range has to be updated after the
pcolormesh
call, then the easiest way isplt.clim
as others have mentioned. Yet another way is through thecolors.Normalize
object of theQuadMesh
/PolyCollection
object created by apcolormesh
/pcolor
call.On a side note,
Quadmesh
resides incollections
of an Axes instance, so if the result of apcolormesh
was not assigned to a variable,pmesh = plt.gca().collections[0]
may be used as well.Using tom10's setup, this can be written as follows.
Instead of having duplicate colorbars for multiple subplots, appending a single one at the right side of the figure may be "prettier". @G M already suggested a solution that does that by hard coding the colorbar boundaries but the boundaries may be set dynamically by using a function defined here.
For the example at hand, it may be written as follows.
当使用 figure 环境时,可以通过使用 colorbar 实例来设置 colorbar 的范围,即 colorbar.ax.set_ylim(low, high) :
Range of colorbar can be set by using instance of colorbar i.e. colorbar.ax.set_ylim(low, high) when using figure environment:
如果您想知道如何使用图像和 plt.imshow() 来执行此操作(就像我找到此答案时一样),请继续阅读。
If you're wondering how to do this with images and
plt.imshow()
(like I was when I found this answer), read on.