在Matplotlib中更改/删除轮廓线的透明度 [英] Changing Transparency of/Remove Contour Lines in Matplotlib

查看:90
本文介绍了在Matplotlib中更改/删除轮廓线的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Contourf绘制一些数据,但是在设置透明度时遇到了麻烦.我希望能够设置填充和线条的透明度,但似乎无法做到这一点.

I am using contourf to plot some data but am having trouble when it comes to setting the transparency. I want to be able to set the transparency of both the fill AND the lines, but cannot seem to do this.

我的代码的简化版本如下:

A simplified version of my code is as follows:

array = np.random.rand(100,100)

#lonit and latit are latitude and longitude grids from an input dataset
LONG, LAT = np.meshgrid(lonit, latit)
longitude,latitude = m(LONG, LAT)

pp = m.contourf(longitude, latitude, imagelist[0], 50,
            cmap='YlOrRd', extend="min", alpha = .5) 

plt.show()

这将输出:

并且您可以看到,尽管将alpha设置为0.5,轮廓线的透明度仍保持为1.有人知道如何修改此透明度,以使其与填充相同的透明度吗?或者,完全删除等高线也可以,但我喜欢 contourf 方法,因为它使可视化更容易(除了上述线!)

and as you can see, despite alpha being set to 0.5, the transparency of the contour lines remains at 1. Does anyone have any idea how to amend this so that they are the same transparency as the fill? Alternatively, removing the contour lines altogether may work, but I like the contourf method as it makes visualisation easier (except with the aforementioned lines!)

我的目标是在下面显示底图,这样线条会增加我的绘图的复杂性,因此想要删除/使它们不可见.

My aim is to display a basemap under so the lines add complexity to my plot and thus want to remove/make them invisible.

提前致谢!

更新:显示更多问题

Alpha设置为1.0:

Alpha set to 1.0:

Alpha设置为0.1:

Alpha set to 0.1:

Alpha 设置为 0.6,AntiAliased 设置为 True:

Alpha set to 0.6 and AntiAliased set to True:

pp = m.contourf(longitude, latitude, imagelist[0], 50,
cmap='YlOrRd', extend="min", alpha = 0.6, antialiased = True) 

这改善了线条,但仍然没有摆脱它们.

This has improved the lines but still not got rid of them.

推荐答案

使用 antialiased = True 时仍然看到的线条实际上不是线条,而是闪耀的背景,因为填充了轮廓不要互相碰触.

The lines you still see when using antialiased = True are actually not lines but the background that is shining through, since the filled contours do not touch each other.

一个非常丑陋的修复方法是将相同的图形绘制两次,但级别略有不同.

One very ugly fix could be to plot the same twice but with slightly different levels.

pp1 = m.contourf(longitude, latitude, imagelist[0], 50, cmap='YlOrRd', extend="min", alpha = 0.3, antialiased = True)
pp2 = m.contourf(longitude, latitude, imagelist[0], 55, cmap='YlOrRd', extend="min", alpha = 0.3, antialiased = True)

请注意,您还必须将 alpha 值一分为二才能获得相同的透明度.您仍然会看到线条,但没有以前那么强烈.

Note, that you also have to divide the alpha value in half to get the same transparency. You will still see the lines, but not as strong as before.

这篇关于在Matplotlib中更改/删除轮廓线的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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