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.
Related
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.
In the following example (modified from here) I get an error regarding the use of the extended keyword when using a log scale:
import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from matplotlib import colors, ticker, cm
from matplotlib.mlab import bivariate_normal
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)
z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0)
+ 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
cs = plt.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r, extend='both')
cbar = plt.colorbar()
plt.show()
I have tried this in versions 1.5.3 and 1.5.1 and the exact error is
ValueError: extend kwarg does not work yet with log scale
This is even said in that page so this is no surprise.
I have tried some workarounds to that, such as cs.cmap.colorbar_extend = True, but, although I get no error, the extended option isn't implemented.
Is there a workaround for this? In other words: can I implement a log scale on contourf even though the keyword isn't implemented yet?
Thank you.
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()
I am working with Python and matplotlib.
I am doing a continuously plot of some data I get from a spectrum analyser. So with every new step of my loop a new piece is added to my plot.
But i just realised that the color scaling doesn't remain the same. So sometimes if the data are a little different it makes a completely new scaling:
You can recognise it at the big red circle in the middle. The value of the entries for the red circle are all zeros. So normally it should have the same color with every new iteration. But as you can see, if you look closely, this isn't the case. Sometimes the red is a little bit darker.
I already set my vminto -100 and my vmax to 0 :
while True:
... #some code
a = np.linspace( (i*np.pi/8-np.pi/16)%(np.pi*2) ,( i*np.pi/8+np.pi/16)%(np.pi*2) , 2)#Angle, circle is divided into 16 pieces
b = np.linspace(start -scaleplot, stop,801) #points of the frequency + 200 more points to gain the inner circle
A, B = np.meshgrid(a, longzeroarray)
ax.contourf(a, b, B , cmap=cm.jet, vmin=-100, vmax=0)
plt.draw()
I hope you have some helpful ideas.
You can specify a fixed levels, for example (modified from doc):
You can't use vmax and vmin together with levels, as the former will override the latter. To get round it, you can use masked array:
import numpy as np
import matplotlib.pyplot as plt
origin = 'lower'
#origin = 'upper'
delta = 0.025
x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10 * (Z1 - Z2)
levels = np.linspace(-3,2,50)
Vmin = -1.
Vmax = 1.
M = np.ma.array(Z, mask=((Z<Vmin)|(Z>Vmax)))
#levels = np.hstack(([Vmin], levels, [Vmax]))
f, (ax1, cax1, ax2, cax2) = plt.subplots(1,4,gridspec_kw={'width_ratios':[9,1,9,1]})
cf1 = ax1.contourf(X, Y, M, levels=levels)
plt.colorbar(cf1, cax=cax1)
cf2 = ax2.contourf(X, Y, M*0.5, levels=levels)
plt.colorbar(cf2, cax=cax2)
I would like to be able to change width of a line according to a list of values. For example if I have the following list to plot:
a = [0.0, 1.0, 2.0, 3.0, 4.0]
could I use the following list to set the linewidth?
b = [1.0, 1.5, 3.0, 2.0, 1.0]
It doesn't seem to be supported, but they say "anything is possible" so thought I would ask people with more experience (noob here).
Thanks
Basically, you have two options.
Use a LineCollection. In this case, your line widths will be in points, and the line width will be constant for each segment.
Use a polygon (easiest with fill_between, but for complex curves you may need to create it directly). In this case, your line widths will be in data units and will vary linearly between each segment in your line.
Here are examples of both:
Line Collection Example
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
np.random.seed(1977)
x = np.arange(10)
y = np.cos(x / np.pi)
width = 20 * np.random.random(x.shape)
# Create the line collection. Widths are in _points_! A line collection
# consists of a series of segments, so we need to reformat the data slightly.
coords = zip(x, y)
lines = [(start, end) for start, end in zip(coords[:-1], coords[1:])]
lines = LineCollection(lines, linewidths=width)
fig, ax = plt.subplots()
ax.add_collection(lines)
ax.autoscale()
plt.show()
Polygon Example:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)
x = np.arange(10)
y = np.cos(x / np.pi)
width = 0.5 * np.random.random(x.shape)
fig, ax = plt.subplots()
ax.fill_between(x, y - width/2, y + width/2)
plt.show()