I have a contour plot application that I'd like to know the pixel location of the axes origin. I've read through the Transformation Tutorial, but it doesn't seem to be working properly.
Here's the code, adapted from the Contour Demo program:
#!/usr/bin/env python
"""
Illustrate simple contour plotting, contours on an image with
a colorbar for the contours, and labelled contours.
See also contour_image.py.
"""
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
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)
# Create a simple contour plot with labels using default colors. The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
plt.figure()
CS = plt.contour(X, Y, Z)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
print "Origin:\t", plt.gca().transData.transform((-3.0, -2.0))
plt.savefig("cdemo.png")
The output is:
Origin: [ 80. 48.]
And the following image:
However, when I look at this with an editor that shows the cursor position in pixels (GIMP) it shows the origin location as (100,540). I understand that Matplotlib's origin is lower left, and GIMP counts from upper left, so adjusting for this with the image size of (800, 600) that gives me a translated location of (100,60).
Any ideas? Here's the image with the approximate location of (80, 48) marked in red at the lower left.
Using matplotlib 1.4.3
Thanks!
tcaswell nailed it - the problem was a mismatch in dpi between the figure object, and the saved image file. figure() defaults to 80 dpi, while savefig() defaults to 100 dpi
So you can fix it two ways...
Change the dpi of the figure() call to match the savefig() default:
plt.figure(dpi=100)
Or you can change the dpi of the savefig() call to match the figure() default:
plt.savefig("cdemo.png", dpi=80)
Related
Say I am plotting a set of points with an image as a background. I've used the Lena image in the example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread
np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,10.0,15)
img = imread("lena.jpg")
plt.scatter(x,y,zorder=1)
plt.imshow(img,zorder=0)
plt.show()
This gives me .
My question is: How can I specify the corner coordinates of the image in the plot? Let's say I'd like the bottom-left corner to be at x, y = 0.5, 1.0 and the top-right corner to be at x, y = 8.0, 7.0.
Use the extent keyword of imshow. The order of the argument is [left, right, bottom, top]
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,10.0,15)
datafile = 'lena.jpg'
img = plt.imread(datafile)
plt.scatter(x,y,zorder=1)
plt.imshow(img, zorder=0, extent=[0.5, 8.0, 1.0, 7.0])
plt.show()
For cases where it's desired to have an image in a small area of the scatter plot, change the order of the plots (.imshow then .scatter) and change the extent values.
plt.imshow(img, zorder=0, extent=[3.0, 5.0, 3.0, 4.50])
plt.scatter(x, y, zorder=1)
plt.show()
You must use the extent keyword parameter:
imshow(img, zorder=0, extent=[left, right, bottom, top])
The elements of extent should be specified in data units so that the image can match the data. This can be used, for example, to overlay a geographical path (coordinate array) over a geo-referenced map image.
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.
I am creating a histogram for my data. Interestingly, when I plot my raw data and their histogram together on one plot, they are a "y-flipped" version of each other as follows:
I failed to find out the reason and fix it. My code snippet is as follows:
import math as mt
import numpy as np
import matplotlib.pylab as plt
x = np.random.randn(50)
y = np.random.randn(50)
w = np.random.randn(50)
leftBound, rightBound, topBound, bottomBound = min(x), max(x), max(y), min(y)
# parameters for histogram
x_edges = np.linspace(int(mt.floor(leftBound)), int(mt.ceil(rightBound)), int(mt.ceil(rightBound))-int(mt.floor(leftBound))+1)
y_edges = np.linspace(int(mt.floor(bottomBound)), int(mt.ceil(topBound)), int(mt.ceil(topBound))-int(mt.floor(bottomBound))+1)
# construct the histogram
wcounts = np.histogram2d(x, y, bins=(x_edges, y_edges), normed=False, weights=w)[0]
# wcounts is a 2D array, with each element representing the weighted count in a bins
# show histogram
extent = x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes.set_xlabel('x (m)')
axes.set_ylabel('y (m)')
histogram = axes.imshow(np.transpose(wcounts), extent=extent, alpha=1, vmin=0.5, vmax=5, cmap=cm.binary) # alpha controls the transparency
fig.colorbar(histogram)
# show data
axes.plot(x, y, color = '#99ffff')
Since the data here are generated randomly for demonstration, I don't think it helps much, if the problem is with that particular data set. But anyway, if it is something wrong with the code, it still helps.
By default, axes.imshow(z) places array element z[0,0] in the top left corner of the axes (or the extent in this case). You probably want to either add the origin="bottom" argument to your imshow() call or pass a flipped data array, i.e., z[:,::-1].
I know this is well documented, but I'm struggling to implement this in my code.
I would like to shade the area under my graph with a colormap. Is it possible to have a colour, i.e. red from any points over 30, and a gradient up until that point?
I am using the method fill_between, but I'm happy to change this if there is a better way to do it.
def plot(sd_values):
plt.figure()
sd_values=np.array(sd_values)
x=np.arange(len(sd_values))
plt.plot(x,sd_values, linewidth=1)
plt.fill_between(x,sd_values, cmap=plt.cm.jet)
plt.show()
This is the result at the moment. I have tried axvspan, but this doesnt have cmap as an option. Why does the below graph not show a colormap?
I'm not sure if the cmap argument should be part of the fill_between plotting command. In your case probably want to use the fill() command btw.
These fill commands create polygons or polygon collections. A polygon collection can take a cmap but with fill there is no way of providing the data on which it should be colored.
What's (for as far as i know) certainly not possible is to fill a single polygon with a gradient as you wish.
The next best thing is to fake it. You can plot a shaded image and clip it based on the created polygon.
# create some sample data
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x) * 120
fig, ax = plt.subplots()
# plot only the outline of the polygon, and capture the result
poly, = ax.fill(x, y, facecolor='none')
# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
# create a dummy image
img_data = np.arange(ymin,ymax,(ymax-ymin)/100.)
img_data = img_data.reshape(img_data.size,1)
# plot and clip the image
im = ax.imshow(img_data, aspect='auto', origin='lower', cmap=plt.cm.Reds_r, extent=[xmin,xmax,ymin,ymax], vmin=y.min(), vmax=30.)
im.set_clip_path(poly)
The image is given an extent which basically stretches it over the entire axes. Then the clip_path makes it only showup where the fill polygon is drawn.
I think all you need is to do the plot of the data one at a time, like:
import numpy
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
# Create fake data
x = numpy.linspace(0,4)
y = numpy.exp(x)
# Now plot one by one
bar_width = x[1] - x[0] # assuming x is linealy spaced
for pointx, pointy in zip(x,y):
current_color = cm.jet( min(pointy/30, 30)) # maximum of 30
plt.bar(pointx, pointy, bar_width, color = current_color)
plt.show()
Resulting in:
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'.