Changing Transparency of/Remove Contour Lines in Matplotlib
Asked Answered
C

1

2

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()

This outputs: enter image description here

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.

Thanks in advance!

UPDATE: MORE GRAPHS TO DISPLAY PROBLEM

Alpha set to 1.0: enter image description here

Alpha set to 0.1: enter image description here

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) 

enter image description here

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

Christenachristendom answered 23/11, 2016 at 15:5 Comment(7)
Are you sure that alpha parameter doesn't change transparency of both ?Sultan
tried lowering the alpha more?Unbroken
Hi both of you, I have updated the question with more images showing difference alpha levels! - Alpha = 0 produces a completely invisible plot which would imply that its transparency is being altered. Maybe I need to remove contour lines altogether so I will add this option to the original question.Christenachristendom
Possible duplicate of Matplotlib Contourf Plots Unwanted Outlines when Alpha < 1Gurule
Thanks @Bart, I have tried Antialiased and added to my answer the results, unfortunately they are still visible, although to a lesser extent.Christenachristendom
I've been struggling with similar problems before; sometimes calling contourf twice seemed to solve the problem (I know, it's ugly...).Gurule
That is indeed ugly but does seem to work, thank you! It's annoying that there isn't an argument to remove the lines because with the fills... the lines are kind of redundant and get in the way....Christenachristendom
A
1

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)

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.

Androecium answered 4/3, 2017 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.