这是生成图解说明我的问题的最小示例:
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
(以提高可读性)。
如何linewidths
在colorbar
不更改图中的情况下更改?
我最初尝试过CB.collections
,但是colorbar
没有collections
。同样,colorbar
使用参数调用linewidths=4.0
也不起作用(这是未知参数)。
注释
在输入此问题时,我有这个主意(橡皮鸭调试):
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
以减小其线宽。
这有效。
但是:有没有一种方法可以直接控制linewidths
中的colorbar
?
您只需要了解如何访问这些行,就可以尝试:
>>> 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
对象。
现在让我们尝试以几种方式对其进行修改:
>>> 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.
结果
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句