I have a calculated data (x,y) which I would like to put on top of an image from a paper to compare with some of its boundaries. If this image was a 2D x,y plot, I would have extracted easily the points and then plot together with mine. However this image is a 3D color plot, therefore if I want just to read the image, define my coordinates according to it and plot my data on top, so at the end would seem like 2 layers.
I can plot scatter points by following:
import matplotlib.pyplot as plt
im = plt.imread('fig_exp.png')
implot = plt.imshow(im)
# put a blue dot at (10, 20)
plt.scatter([10], [20], c='r', s=2000, marker=u'*')
plt.show()
But how can I define image coordinates so I can plot e.g
x=[-100,-80,-60,-40]
y=[10,15,20,25]
plt.plot(x,y,'-ro')
The problem here is that I dont know how to define the coordinates as in the figure. Beside I dont know how to plot not as scatter but a line.
I appreciate any helpful solution!
You are looking for the extent= keyword argument to imshow() as detailed in the documentation
As for your second question, if you want a line plot, you use the plot() function instead of scatter().
Related
I have plotted some data that are defined by specific coordinates and a value.
I have plotted this with a scatterplot, and would now like to plot this on top of an image, to reflect where the data points are located.
I have tried to use matplotlib.image and import img to be able to show the image. Unfortunately, the image of my location does not match up with the data completely (due to stretching, I believe).
When I edit the extent Python either edits out some of the image, instead of moving my data. I would like to be able to move the image/crop it to fit with the data or use some sort of package to load in a map (as I have the coordinates). This needs to show a local stream in Denmark.
x and y are our coordinates (lat, lon) and z is the value each coordinate have.
plt.figure(figsize=(20,10))
plt.scatter(x,y, cmap = 'gist_heat' ,s=100,c=z)
plt.colorbar()
plt.ylim(np.min(y),np.max(y))
plt.xlim(np.min(x),np.max(x))
img = plt.imread("grindsted4_sattelit.png")
ext = [np.min(x),np.max(x), np.min(y), np.max(y)]
plt.imshow(img, zorder=0, extent=ext)
aspect=img.shape[0]/float(img.shape[1])*((ext[1]-ext[0])/(ext[3]-ext[2]))
plt.gca().set_aspect(aspect)
plt.show()
The plot are the colored dots, and the black and white dots are my coordinates in Google Earth.
Thank you!
I had some data on which I had to do some image processing to obtain the centers of the 4 'circles' (refer figure). I was able to do that correctly using the Blob detection function of the OpenCV library in Python.
I want to plot the lines perpendicular to the x and y axis to indicate these centers on the seaborn heatmap output. I'm unable to do so. I have the co-ordinates for the lines in a separate list.
But I'm unable to plot even one line to begin with.
plt.figure(figsize = (12,8))
sns.heatmap(df_scaled)
plt.axvline(x = -10.86,color='white',linewidth=2) # For now let's assume value of x as -10.86
plt.tight_layout()
plt.show()]
I am working on a visualization script for a linear algebra class at the university and I am trying to show multiple vectors using the quiver function in python. I am trying to plot vectors coming from a 2x2 matrix in one quiver function, however, now that I am trying to label them I would like to access each vector individually.
import numpy as np
import matplotlib.pyplot as plt
A = np.array([[1,3], [2,2]])
# create figure
fig = plt.figure()
# creates variable containing current figure
ax = fig.gca()
baseArrow = ax.quiver(*origin, A[0,:], A[1,:], color=['r','g']', angles='xy', scale_units='xy', scale=1)
ax.quiverkey(baseArrow,.85,.85,0.8,'i-hat',labelcolor='k',labelpos='S', coordinates = 'figure')
# display grid
plt.grid()
# display figure
plt.show()
This alows me to label the first vector with the respective color (red). Now what I would like to do is label the second vector in green with a different label?
Maybe something like:
ax.quiverkey(baseArrow**[2]**,.85,.85,0.8,'i-hat',labelcolor='k',labelpos='S', coordinates = 'figure')
Is there any way to pull out each vector by itself or would it be better to plot them individually instead of as a vector? I looked at the following question but it doesn't really solve my issue. Matplotlib Quiver plot matching key label color with arrow color
My feeling is that the quiver function is better suited/intended to plot numerous vectors as you would find in a graph depicting magnetic forces, vortices (sic) or gradients (see meshgrid for example). And it's API reflects that, in that it accepts end and start coordinates separately: i.e. you need to split the components of your vectors as you have done above.
May I suggest you look into the plot or arrow functions which will give you greater control over your visualization (e.g. vector-independent labels) and will also provide greater clarity in your code, as you will be able to declare vectors (as np.arrays of course) and use them directly.
Finally note that you can obtain fig and ax in one call: fib, ax = plt.subplots().
Hope this helps!
I have a set of pixel values that I wanted to map on an image as a heatmap. My pixel values look something like this: they are x,y coordinates of image pixels of an arbitrary resolution.
pixel values = [[1,1],[2,1],[3,1],[4,1],[2,1]]
I have tried using OpenCV but I fail to understand how to get it to work. I assume a probability/density distribution needs to be generated, or perhaps the plotting function can do this automatically ? Since the image is loaded using OpenCV, I was looking for an OpenCV function, if Matplotlib works, please do comment.
heatmap_array = np.array(heatmap)
cv_colormap = cv2.applyColorMap(heatmap_array[0:], cv2.COLORMAP_HOT)
cv2.imwrite("colormap_gen.jpg", cv_colormap)
There needs to be an slightly opaque overlay of the heatmap on top of the original image.
You can try with Matplotlib:
# sample data
# xy can be np.array(pixel_values)
np.random.seed(1)
xy = np.random.multivariate_normal([300,300], [[2000,400],[400,1000]], 2000000)
# compute the hist
# bins here are the size of the image
hist,_,_ = np.histogram2d(xy[:,0], xy[:,1], bins=(600,800))
# show heatmap by plt
# you can use plt to save the figure
fig = plt.figure(figsize=(12,8))
plt.imshow(hist,cmap='hot')
plt.axis('off')
plt.show()
Output:
Here is the code:
plots=imshow(Z,extent=extent,origin,cmap=cmap,aspect='auto',vmin=vmin,vmax=vmax)
plots.plot(Response,component,vrange)
It plots an image based on data list Z, how can I let it print data points instead of an image?
Looks like needs to change to scatter(x, y,...) to plot data points, how difficult it is to change array Z to x, y?
As #jdj081 said, you want to produce a scatter plot.
import os.path
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
# get an image from the sample data directory
fname = os.path.join(matplotlib.get_data_path(), 'sample_data', 'lena.png')
im = plt.imread(fname)
# Reduce the data by a factor of 4 (so that we can see the points)
im = im[::4, ::4]
# generate coordinates for the image. Note that the image is "top down", so the y coordinate goes from high to low.
ys, xs = np.mgrid[im.shape[0]:0:-1, 0:im.shape[1]]
# Scatter plots take 1d arrays of xs and ys, and the colour takes a 2d array,
# with the second dimension being RGB
plt.scatter(xs.flatten(), ys.flatten(), s=4,
c=im.flatten().reshape(-1, 3), edgecolor='face')
plt.show()
You didn't provide much information to go on, but it sounds like you really want to create a scatter plot.
There are many options here depending on what you are plotting and what you want to see, but I have found the following helpful:
Fixing color in scatter plots in matplotlib
import pylab
pylab.figure(1)
pylab.plot([1,2,3,4],[1,7,3,5]) # draw on figure one
pylab.show() # show figure on screen