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.
Related
I have an image that consists of float values and another one that consist only of ones and zeros. I want to plot the second image over the first one, but I only want to plot the ones from the second image. The zeros shall not be plotted.
Ì have tried the following code and I also changed the alpha of y to 1. The problem is, that either the red windows of y are changed from x (alpha of y = 0.5), or one can not even see the plots of x (alpha of y=1).
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random(size=(20,20))
y = np.random.randint(2, size=(20,20))
fig = plt.figure()
plt.imshow(x, cmap="Greys", alpha = 0.5)
plt.imshow(y, cmap="Reds", alpha = 0.5)
plt.show()
How can I only plot the ones of y?
UPDATE:
Thank you for your answers! But this is not want I am looking for. I will explain again:
The result should be something like: x as background and every position, where y is 1, should be colored pure red.
Following the approach in this answer linked by #ImportanceOfBeingEarnest, the exact solution in your case would look like below. Here, np.ma.masked_where will mask your y array at places where it is 0. The resulting array will only contain 1.
EDIT: The problem of overlaying seems to stem from the choice of cmap. If you don't specify the cmap for the y, you can clearly see below that indeed only 1's are plotted and overlaid on the top of x. In order to have a discrete color (red in your case), you can create a custom color map
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
x = np.random.random(size=(20,20))
y = np.random.randint(2, size=(20,20))
y_new =np.ma.masked_where(y==0, y)
cmap = colors.ListedColormap(['red'])
fig = plt.figure()
plt.imshow(x, cmap="Greys", alpha = 0.5)
plt.imshow(y_new, cmap=cmap, alpha=1)
plt.show()
We can inverse "The result should be [..] x as background and every position, where y is 1, should be colored pure red.", namely to just plot x, masked by y and set the background to red.
import matplotlib.pyplot as plt
import numpy as np
y = np.random.randint(2, size=(20,20))
x = np.random.random(size=(20,20))
X = np.ma.array(x, mask=y)
fig = plt.figure()
plt.imshow(X, cmap="Greys")
plt.gca().set_facecolor("red")
plt.show()
There are of course related Q&As like
Matplotlib imshow: how to apply a mask on the matrix or
How can I plot NaN values as a special color with imshow in matplotlib?
and there is also an example on the matplotlib page: Image masked
enter image description hereI hope I have not over-looked as previously asked question. I don't think so.
I have an image of a spectrum. I have several laser lines for calibration. Since the laser lines and the spectrum were collected in the same way they should be correlated in pixel distance. The relationship between pixel number and wavelength is nonlinear. I have fit the pixel number along the x-axis against the wavelength of the laser lines (blue # 405nm green # 532nm and red # 650nm) using a 3rd degree polynomial with high correlation. I want to plot the spectrum by computing the wavelength( nm) directly from the pixel number and display the wavelength beneath the spectrum. Is this possible without overlapping the image on another figure? spectrograph of Laser Lines
import matplotlib.pyplot as plt
from scipy import ndimage
from pylab import *
import numpy as np
import skimage
image= laser_lines
print(image.shape)
for i in range(image.shape[1]):
x=i^3*-3.119E-6+2.926E-3*i^2+0.173*i+269.593
for j in range(image.shape[0]):
y=image[i,j]
imshow(image)
plt.show()
Probably the easiest option is to use a pcolormesh instead of an imshow plot. The pcolormesh shows the edges of a grid, such that you might simply transform the original grid using the functional dependence between pixels and wavelength to define the edges of each pixel in terms of wavelength.
import numpy as np
import matplotlib.pyplot as plt
image = np.sort(np.random.randint(0,256,size=(400,600)),axis=0)
f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)
Xw = f(Xi)
fig, (ax) = plt.subplots(figsize=(8,4))
ax.pcolormesh(Xw, Yi, image)
ax.set_xlabel("wavelength [nm]")
plt.show()
If the image has 3 colorchannels, you need to use the color argument of pcolormesh to set the color of each pixel, as shown in this question: Plotting an irregularly-spaced RGB image in Python
import numpy as np
import matplotlib.pyplot as plt
r = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
g = np.sort(np.random.randint(0,256,size=(200,600)),axis=0)
b = np.sort(np.random.randint(0,256,size=(200,600)),axis=1)
image = np.dstack([r, g, b])
color = image.reshape((image.shape[0]*image.shape[1],image.shape[2]))
if color.max() > 1.:
color = color/255.
f = lambda i: i**3*-3.119E-6+2.926E-3*i**2+0.173*i+269.593
xi = np.arange(0,image.shape[1]+1)-0.5
yi = np.arange(0,image.shape[0]+1)-0.5
Xi, Yi = np.meshgrid(xi, yi)
Xw = f(Xi)
fig, (ax) = plt.subplots(figsize=(8,4))
pc = ax.pcolormesh(Xw, Yi, Xw, color=color )
pc.set_array(None)
ax.set_xlabel("wavelength [nm]")
plt.show()
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)
Consider a variable x containing a floating point number. I want to use matplotlib's colormaps to map this number to a color, but not plot anything. Basically, I want to be able to choose the colormap with mpl.cm.autumn for example, use mpl.colors.Normalize(vmin = -20, vmax = 10) to set the range, and then map x to the corresponding color. But I really don't get the documentation of mpl.cm, so if anyone could give me a hint.
It's as simple as cm.hot(0.3):
import matplotlib.cm as cm
print(cm.hot(0.3))
(0.8240081481370484, 0.0, 0.0, 1.0)
If you also want to have the normalizer, use
import matplotlib as mpl
import matplotlib.cm as cm
norm = mpl.colors.Normalize(vmin=-20, vmax=10)
cmap = cm.hot
x = 0.3
m = cm.ScalarMappable(norm=norm, cmap=cmap)
print(m.to_rgba(x))
(1.0, 0.8225486412996345, 0.0, 1.0)
You can get a color from a colormap by supplying an argument between 0 and 1, e.g. cm.autumn(0.5).
If there is a normalization instance in the game, use the return of the Normalization instead:
import matplotlib.cm as cm
from matplotlib.colors import Normalize
cmap = cm.autumn
norm = Normalize(vmin=-20, vmax=10)
print cmap(norm(5))
Number value to colormap color
import matplotlib.cm as cm
import matplotlib as matplotlib
def color_map_color(value, cmap_name='Wistia', vmin=0, vmax=1):
# norm = plt.Normalize(vmin, vmax)
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
cmap = cm.get_cmap(cmap_name) # PiYG
rgb = cmap(norm(abs(value)))[:3] # will return rgba, we take only first 3 so we get rgb
color = matplotlib.colors.rgb2hex(rgb)
return color
Is it possible to have a standard rectangular (aka Cartesian) plot, but displayed on a polar set of axes?
I just want the masking appearance of the grey border provided by the polar() function, but I do not want to convert my coordinates to polar, and use polar().
If you just want to be able to call two arrays in rectangular coordinates, while plotting on a polar grid, this will give you what you need. Here, x and y are your arrays that, in rectangular, would give you an x=y line. It returns the same trend line but on in polar coordinates. If you require more of a mask, that actually limits some of the data being presented, then insert a line in the for loop that says something like: for r < 5.0: or whatever your criteria is for the mask.
from math import cos, sin, atan2, sqrt
import matplotlib.pyplot as plt
plt.clf()
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
x = range(10)
y = range(10)
r = []
phi = []
for ii in range(len(x)):
r.append(sqrt(x[ii]**2.+y[ii]**2.))
phi.append(atan2(y[ii],x[ii]))
ax.scatter(phi,r)
show()