change dash style of negative values in a default contour - matplotlib - python

I am trying to plot contours with matplotlib and I have negative values in the data and I want them to be dashed(which matplotlib does by default) however, I want to (1) control the dash style (on,off) and (2) change the color for the negative contours alone. I tried the answer in link: How can I set the dash length in a matplotlib contour plot
But this sets all the lines in the contour to dashes which I do not want. I need to hack the negative contour line styles alone!
A part of my code:
from pylab import *
import matplotlib
import numpy as np
matplotlib.rcParams['contour.negative_linestyle']= 'dashed'
CS = ax1.contour(xi, yi, W_t, levels=levels, colors='k', linewidths=0.05)
for c in CS.collections:
c.set_dashes([(0, (2.0, 2.0))])

You can loop though the line collections created by the CS object and for any non solid lines (from get_linetype with value [(None, None)]) set them however you want. As a minimal example,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
#Dummy data
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
CS = plt.contour(X, Y, Z, 20, colors='k')
for line in CS.collections:
if line.get_linestyle() == [(None, None)]:
print("Solid Line")
else:
line.set_linestyle([(0, (12.0, 3.0))])
line.set_color('red')
plt.show()

Related

How to color contour labels by a colormap?

I have a contour plot that I color using the Yellow-Green color map named YlGn
The labels at the darker fields do not appear well, as they are black.
Is there a method to color the labels in the inverse of the used colormap? i.e., to color the 0.39 label in white, and the 0.15 label in dark green, and the labels in-between accordingly.
I used CS3 = plt.contourf(X, Z, M, levels, cmap=plt.cm.YlGn, extend='both') for the filled contour and CS4 = plt.contour(CS3, colors=('k',), linewidths=(1,)) for the line contour, and finally plt.clabel(CS4, linewidths=2, fmt='%2.2f', colors='k', fontsize=14) for the labels.
However when I tried to add cmap=plt.cm.YlGn_r and removed the colors='k' to the labels (to reverse the colors) it did nothing.
Note: The codes used here are partially taken from this documentation page, but with some modifications to fit my data.
Here are some data to try at a Jupyter notebook:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
delta = 0.025
x = np.arange(1.0, 3.0, delta)
y = np.arange(1.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS = plt.contour(X, Y, Z, cmap=plt.cm.YlGn_r)
CS2 = plt.contourf(X, Y, Z, color='k')
plt.clabel(CS, fontsize=10,color='k')
plt.title('Simplest default with labels')
I guess you mixed up the arguments to contour and contourf. Applying the reverse colormap to contour works fine.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
delta = 0.025
x = np.arange(1.0, 3.0, delta)
y = np.arange(1.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS2 = plt.contourf(X, Y, Z, cmap=plt.cm.YlGn_r)
CS = plt.contour(X, Y, Z, cmap=plt.cm.YlGn)
plt.clabel(CS, fontsize=10)
plt.title('Simplest default with labels')
plt.show()
To use the same colormap for the lines as for the fills, but then use a different colormap for the labels, you need to define the colors manually. But the use of the existing levels helps you do that quite efficiently.
CS2 = plt.contourf(X, Y, Z, cmap=plt.cm.YlGn_r)
CS = plt.contour(CS2, cmap=plt.cm.YlGn_r)
plt.clabel(CS, fontsize=10, colors=plt.cm.Reds(CS.norm(CS.levels)))

Colorbar limits are not respecting set vmin/vmax in plt.contourf. How can I more explicitly set the colorbar limits?

Getting a strange result when trying to adjust the data range when plotting using contourf
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS = plt.contourf(X, Y, Z, vmin = 0, vmax = 3)
plt.title('Simplest default with labels')
plt.colorbar()
plt.show()
Results in this for me:
It's like the colors match the vmin/vmax I set, but the number range displayed on the colorbar remains what it would be without setting vmin/vmax.
In this case, I would like the end result to have a colorbar that ranges from 0 to 3.
First of all, the response, marked as answer, is erroneous (see my comments above), but helped me to come up with two other solutions.
As JulianBauer pointed out in a comment below, the function mlab.bivariate_normal used by the OP is not available any more.
To provide functional code that produces output that can be compared with the other answers I am calling the following function, with the definition of bivariate_normal copied from the matplotlib repository:
def myfunction():
def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0):
"""copied from here: https://github.com/matplotlib/matplotlib/blob/81e8154dbba54ac1607b21b22984cabf7a6598fa/lib/matplotlib/mlab.py#L1866"""
Xmu = X-mux
Ymu = Y-muy
rho = sigmaxy/(sigmax*sigmay)
z = Xmu**2/sigmax**2 + Ymu**2/sigmay**2 - 2*rho*Xmu*Ymu/(sigmax*sigmay)
denom = 2*np.pi*sigmax*sigmay*np.sqrt(1-rho**2)
return np.exp(-z/(2*(1-rho**2))) / denom
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
return X,Y,Z
1. A simple and straight forward solution
Make use of the extend command while providing custom levels:
import numpy as np
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
X,Y,Z = myfunction()
plt.figure()
plt.title('Simplest default with labels')
levels = np.linspace(0.0, 3.0, 7)
CS = plt.contourf(X, Y, Z, levels=levels, cmap=cm.coolwarm, extend='min')
colorbar = plt.colorbar(CS)
plt.show()
2. A more complicated solution
is provided in the answer above, though it needs to be adapted to specific cases and one can easily end up with a colorbar whose levels differs from those in the actual plot. I find this dangerous, so I attempted to wrap it up in a function that can safely be called in any context:
def clippedcolorbar(CS, **kwargs):
from matplotlib.cm import ScalarMappable
from numpy import arange, floor, ceil
fig = CS.ax.get_figure()
vmin = CS.get_clim()[0]
vmax = CS.get_clim()[1]
m = ScalarMappable(cmap=CS.get_cmap())
m.set_array(CS.get_array())
m.set_clim(CS.get_clim())
step = CS.levels[1] - CS.levels[0]
cliplower = CS.zmin<vmin
clipupper = CS.zmax>vmax
noextend = 'extend' in kwargs.keys() and kwargs['extend']=='neither'
# set the colorbar boundaries
boundaries = arange((floor(vmin/step)-1+1*(cliplower and noextend))*step, (ceil(vmax/step)+1-1*(clipupper and noextend))*step, step)
kwargs['boundaries'] = boundaries
# if the z-values are outside the colorbar range, add extend marker(s)
# This behavior can be disabled by providing extend='neither' to the function call
if not('extend' in kwargs.keys()) or kwargs['extend'] in ['min','max']:
extend_min = cliplower or ( 'extend' in kwargs.keys() and kwargs['extend']=='min' )
extend_max = clipupper or ( 'extend' in kwargs.keys() and kwargs['extend']=='max' )
if extend_min and extend_max:
kwargs['extend'] = 'both'
elif extend_min:
kwargs['extend'] = 'min'
elif extend_max:
kwargs['extend'] = 'max'
return fig.colorbar(m, **kwargs)
The main commands in the function correspond to what kilojoules proposes in his/her answer, but more lines are required to avoid all the explicit and potentially erroneous assignments by extracting all information from the contourf object.
Usage:
The OP asks for levels from 0 to 3. The darkest blue represents values below 0, so I find an extend-marker useful.
import numpy as np
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
X,Y,Z = myfunction()
plt.figure()
plt.title('Simplest default with labels')
CS = plt.contourf(X, Y, Z, levels=6, vmin=0.0, vmax=3.0, cmap=cm.coolwarm)
colorbar = clippedcolorbar(CS)
plt.show()
The extend marker can be disabled by calling clippedcolorbar(CS, extend='neither') instead of clippedcolorbar(CS).
We can explicitly set the colorbar limits by sending a scalar mappable to colorbar.
CS = plt.contourf(X, Y, Z, 5, vmin = 0., vmax = 2., cmap=cm.coolwarm)
plt.title('Simplest default with labels')
m = plt.cm.ScalarMappable(cmap=cm.coolwarm)
m.set_array(Z)
m.set_clim(0., 2.)
plt.colorbar(m, boundaries=np.linspace(0, 2, 6))
Edit
See Bastian's answer for a complete solution. The problem with my approach is that the segments of the color bar don't correspond to the segments of the contour plot. They use the same coloring scale, but the contour plot and color bar have divided the color scale in different ways. Using the correct lower/upper bounds, this solution gives 6 levels of the contour plot and 6 levels of the colorbar. Since the contour plot and color bar have different bounds, the color segments are different.

Adding extra contour lines using matplotlib 2D contour plotting

I am creating a two-dimensional contour plot with matplotlib. Using the documentation provided http://matplotlib.org/examples/pylab_examples/contour_demo.html, such a contour plot can be created by
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
which outputs the following plot.
The documentation details how to manually label certain contours (or "lines") on the existing plot. My question is how to create more contour lines than those shown.
For example, the plot shown has two bivariate gaussians. The upper right has three contour lines, at 0.5, 1.0, and 1.5.
How could I add contour lines at say 0.75 and 1.25?
Also, I should be able to zoom in and (in principle) add dozens of dozens of contour lines from (for example) 1.0 and 1.5. How does one do this?
To draw isolines at specified level values, set the levels parameter in .contour:
levels = np.arange(-1.0,1.5,0.25)
CS = plt.contour(X, Y, Z, levels=levels)
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
levels = np.arange(-1.0,1.5,0.25)
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('levels = {}'.format(levels.tolist()))
plt.show()
The sixth figure here uses this method to draw isolines at levels = np.arange(-1.2, 1.6, 0.2).
To zoom in, set the x limits and y limits of the desired region:
plt.xlim(0, 3)
plt.ylim(0, 2)
and to draw, say, 24 automatically-chosen levels, use
CS = plt.contour(X, Y, Z, 24)
For example,
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
N = 24
CS = plt.contour(X, Y, Z, N)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('{} levels'.format(N))
plt.xlim(0, 3)
plt.ylim(0, 2)
plt.show()
The third figure here uses this method to draw 6 isolines.

How can I set the dash length in a matplotlib contour plot

I'm making some contour plots in matplotlib and the length of the dashes are too long. The dotted line also doesn't look good. I'd like to manually set the length of the dash. I can set the exact dash length when I'm making a simple plot using plt.plot(), however I cannot figure out how to do the same thing with a contour plot.
I think that the following code should work, but I get the error:
File "/Library/Python/2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/backend_macosx.py", line 80, in draw_path_collection
offset_position)
TypeError: failed to obtain the offset and dashes from the linestyle
Here is a sample of what I'm trying to do, adapted from the MPL examples:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.figure()
CS = plt.contour(X, Y, Z, 6, colors='k',linestyles='dashed')
for c in CS.collections:
c.set_dashes([2,2])
plt.show()
Thanks!
Almost.
It's:
for c in CS.collections:
c.set_dashes([(0, (2.0, 2.0))])
If you had put a print c.get_dashes() there, you would have found out (it's what I did).
Perhaps the definition of the line style has changed a bit, and you were working from an older example.
The collections documentation has this to say:
set_dashes(ls)
alias for set_linestyle
set_linestyle(ls)
Set the linestyle(s) for the collection.
ACCEPTS: [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) ]
So in [(0, (2.0, 2.0))], 0 is the offset, and then the tuple is the on-off repeating pattern.
Although this is an old question, I had to deal with it and the current answer is not valid anymore. A better approach is to use plt.rcParams['lines.dashed_style'] = [2.0, 2.0] before your plot.

Overlaying a scatter plot on background image and changing axes ranges

I have a set of x,y map coordinates that I want plotted on an background image of the map.
I use the following code to display my map:
import matplotlib.pyplot as plt
im=plt.imread('map.gif')
implot=plt.imshow(im,origin='lower')
Now the ranges for the x and y axes are the pixel values of the image. In my case, these are:
im.shape[0]
545
im.shape[1]
1011
So the x-axis of the plot goes from 0 to 1011 and the y-axis from 0 to 545.
The map actually covers a range from -100 to +100 in the x-axis and -50 to 50 in the y-axis and my x,y coordinate values are on the same system.
How can I get the x-axis of the plot going from -100 to +100 and not 0 to 1011? and then overplot my x,y scatter plot.
The following code, from the matplotlib site, shows a plot that goes from -3 to 3, check it out:
#!/usr/bin/env python
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2-Z1 # difference of Gaussians
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3,3,-3,3])
plt.show()
The important part is the 'extent' argument of 'imshow'.

Categories

Resources