Matplotlib:如何为等高线图调整颜色条中的线宽? [英] Matplotlib: How to adjust linewidth in colorbar for contour plot?

查看:125
本文介绍了Matplotlib:如何为等高线图调整颜色条中的线宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是生成说明我的问题的图的最小示例:

Here's a minimal example to generate a plot which illustrates my question:

import matplotlib.pylab as plt
import matplotlib.mpl as mpl
import numpy as np
import random

data = [[random.random() for i in range(10)] for j in range(10)]

[XT, YT] = np.meshgrid(np.arange(1,10+1,1), np.arange(1,10+1,1))

cmap = mpl.cm.gray

fig, ax = plt.subplots()

CS = ax.contour(XT, YT, data,levels=np.arange(0,1+0.1,0.1),\
                cmap=cmap,linewidths=0.75)
CB = plt.colorbar(CS, ticks=np.arange(0,1+0.1,0.1))  

plt.show()

结果图如下所示:

我想将图中轮廓线的 linewidths 保持在 0.75 ,但在 colorbar 中增加它们(以提高可读性)).

I would like to keep the linewidths of the contour lines in the figure at 0.75 but increase them in the colorbar (for better readability).

如何更改 colorbar 中的 linewidths ,而不更改图中的它们?

How do I change the linewidths in the colorbar without changing them in the figure?

我最初尝试使用 CB.collections ,但 colorbar 没有 collections .此外,使用参数 linewidths=4.0 调用 colorbar 不起作用(这是一个未知参数).

I initially tried CB.collections, but the colorbar has no collections. Also, calling colorbar with an argument linewidths=4.0 does not work (it's an unknown parameter).

评论
键入此问题时,我有这个主意(橡皮鸭调试):

CS = ax.contour(XT, YT, data,levels=np.arange(0,1+0.1,0.1),\
            cmap=cmap,linewidths=4.0)
CB = plt.colorbar(CS, ticks=np.arange(0,1+0.1,0.1))
plt.setp(CS.collections , linewidth=0.75)

基本上,将初始 linewidths 设置为 colorbar 所需的级别,然后生成 colorbar,然后使用 collections 在原始轮廓线上以减少其线宽.
这行得通.

Basically, setting the initial linewidths to the desired level for the colorbar, then generating the colorbar and afterwards using collections on the original contour-lines to decrease their linewidth.
This works.

但是:有没有办法直接控制colorbar中的linewidths?

But: Is there a way to directly control the linewidths in the colorbar?

推荐答案

您只需要了解如何访问这些行,就可以尝试:

You just need to find out how to access those lines, let try:

>>> CB.ax.get_children()
[<matplotlib.axis.XAxis object at 0x026A74B0>, <matplotlib.axis.YAxis object at 0x026AF270>, <matplotlib.lines.Line2D object at 0x026AF190>, <matplotlib.patches.Polygon object at 0x027387F0>, <matplotlib.collections.LineCollection object at 0x02748BD0>, <matplotlib.text.Text object at 0x026C0D10>, <matplotlib.patches.Rectangle object at 0x026C0D50>, <matplotlib.spines.Spine object at 0x026A7410>, <matplotlib.spines.Spine object at 0x026A7290>, <matplotlib.spines.Spine object at 0x026A7350>, <matplotlib.spines.Spine object at 0x026A71B0>]

好吧,猜猜看,我敢打赌第 5 项是分隔线列表.我们正在寻找一些 .line 对象,并且有两个.第一个(第三项)实际上是整个颜色条的边缘(如果我没记错的话).因此,我将使用下一个 .line 对象.

Alright, take a guess, I bet the 5th item is a list of the divider lines. We are looking for some .line objects and there are two. The first one (3rd item) actually is the edge of the entire color bar (if I remember correctly). So I will go for the next .line object.

现在让我们尝试以几种方式对其进行修改:

Now let's try to modified it in a few ways:

>>> len(lines1[4].get_linewidths())
11 #how many item are there? 11 lines
>>> lines1[4].set_color(['r']*11) #set them all to red, in this example we actually want to have the color stay the same, this is just for a demonstration. 
>>> lines1[4].set_linewidths([2]*11) #set them all to have linewidth of 2.

结果

这篇关于Matplotlib:如何为等高线图调整颜色条中的线宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆