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.

tda
  • 2,045
  • 1
  • 17
  • 42
  • Are you sure that `alpha` parameter doesn't change transparency of both ? – Chr Nov 23 '16 at 15:12
  • tried lowering the alpha more? – Taufiq Rahman Nov 23 '16 at 15:18
  • 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. – tda Nov 23 '16 at 15:24
  • Possible duplicate of [Matplotlib Contourf Plots Unwanted Outlines when Alpha < 1](http://stackoverflow.com/questions/15192661/matplotlib-contourf-plots-unwanted-outlines-when-alpha-1) – Bart Nov 23 '16 at 15:35
  • Thanks @Bart, I have tried Antialiased and added to my answer the results, unfortunately they are still visible, although to a lesser extent. – tda Nov 23 '16 at 15:42
  • 1
    I've been struggling with similar problems before; sometimes calling `contourf` twice seemed to solve the problem (I know, it's ugly...). – Bart Nov 23 '16 at 15:44
  • 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.... – tda Nov 23 '16 at 15:50

1 Answers1

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.

Sebastian
  • 755
  • 3
  • 7
  • 22