I have some code that eventually produces a contourf plot at a certain location (lat/lon), like below:
The purple in the image represents the matplotlib plot, which is then overlain on a vector world shapefile. Here, it can be seen that the plot is shifted to the left and up of the location on the vector (blue background). The center location on the vector is the red 'X' and the same coordinates on the matplotlib plot is the red '+'. I first thought this shift was coming from some PyQGIS code, but now I think its the matplotlib commands I have in my Python code.
The commands that I use to create the plot and then save the plot to become a .png image are below:
plt.contourf(Xa,Ya,Result)
plt.grid(color='w')
plt.subplots_adjust(left=0,bottom=0,right=1,top=1,wspace=0,hspace=0)
filename="Results/submerged.png"
plt.savefig(filename, dpi=599, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False)
Where Xa and Ya are the grid coordinates and Result is the result that is plotted.
I then take the saved .png file and overlay it on the vector in PyQGIS. I asked a question on stackexchange about the shifted result here, but haven't gotten any responses.
Any suggestions would be helpful!
Related
I've been stuck for a few days trying to plot a TIF file over a basemap (Google Maps or Mapbox) using Python. I don't know if this is actually possible because I didn't find examples and nothing very clear about it, the most I found is how to plot vector files (shp) on top of basemaps, but nothing about raster.
I have a raster (rainfall_clipped.tif) that is in EPSG:4326 and I'm trying to plot it on top of a MapBox map using the Contextily package. At first I suspected that it could be a projection problem, but even doing the conversions to EPSG:3857, it didn't work. In the documentation Contextily says that it accepts both projections.
Contextily seems to understand the projections, even because it generates the map with the correct axes, the big problem is that it doesn't plot the TIF file on the map. And it's also reading the raster because it loaded the colorbar with the correct values. I don't know if the layer order is wrong or what else it could be.
Below is the code I am using and then the image that is being generated:
data = rasterio.open("rainfall_clipped.tif")
# Read the bounds
left, bottom, right, top = data.bounds
# Create a figure and axes
fig, ax = plt.subplots(figsize=(10, 10))
# Add the raster data to the plot using imshow
im = ax.imshow(data.read(1), extent=[left, right, bottom, top], cmap="Blues")
# Add a colorbar
fig.colorbar(im, ax=ax)
# Add a basemap using contextily
ctx.add_basemap(ax, crs=data.crs, source=ctx.providers.MapBox(accessToken="my_key", id="mapbox/satellite-v9"))
# Show the plot
plt.show()
This code is generating this image:
Output image
What I'm trying to do is something like below:
Expected image
Any suggestions what I can do?
I am currently working on a 3D simulation data. I have a 3D surface, for simplicity, lets say, I have a hemispherical surface. So naturally, I have all the (x,y,z) coordinates that make up the surface. Now I also have a fourth array having the values of some variable (say Pressure for example) at all the (x,y,z) locations that make up the hemispherical surface. My aim is to plot the hemispherical surface and the surface should be coloured according to the fourth array (i.e according to the value of Pressure at that surface).
I have tried pyplot.scatter function from matplotlib, where i use pyplot.scatter(x,y,z, c= Pressure_array) but it leaves me with an artefact like the one shown below (image shows a zoomed in portion of the entire plot)
Notice the fringe like circular pattern. This arises because a Cartesian grid is sampled by a spherical surface and the same is plotted by the scatter points. This pattern remains even upon interpolation of the color values
I am looking for an alternative to the scatter plot method where the surface will be smoother and the circular fringes will be absent. I am aware that matplotlib has surface plots, but i am unable to use it because there, the 'z' coordinate sets both, the height of the plot in 3D and essentially the Color of the surface as well.
Any alternative to scatter plot or surface plot, or a way to get the same domne with the surface plot function in matplotlib will be much appreciated.
Please see below image:
I set the figure size of plt equal to something like (4,6) and set axis to off and margin to zero.
Then continue to draw polyline using coordinate array by ax.plot(line[:,1],line[:,0])
after this I don’t use the plt.show()
But convert the plot to numpy array which has correct (4,6) size but surprisingly fill the plot by stretched to only bbox of the draw line
How can i see all the unused space of figure?
Is there any flag that i have to change in somewhere in matplotlib?
Any help appreciated
The plot of matplotlib will define the output based on shape and size of drawing not based on the pre-defined figure size,some kind of back-end and front-end, or simply it is a responsive-layout when you resize the window everything will scale else those are passed through linewidth= and ...
then i changed my workflow and problem solved ;)
I am plotting a distribution of variables that are outputs from two different versions of a program. They look very similar (this is great because they should!) and I am showing their ratio in the same figure but on a different axis. My goal is to show the ratio as a scatter plot but with a horizontal line at y=1.0 to show 100% agreement. The issue I am having is even if I plot the line first and then the scatter, my scatter points still show underneath the line plot. (Please see the image linked below.) You can see the scatter in black underneath the line plot in red, even though I call the plot function first. Any recommendations? Thank you!
Distribution of two variables with ratio plot underneath
I am plotting a scatterplot on a tkinter canvas and have a basic call such as the following which works great
ax.scatter(_x, _y, _z, marker=_m, c=_c ,s=_s, picker=True, alpha=1)
My questions is, is it possible for a plot point to have two colors, ie instead of a red circle, have a half red/half green circle.
thanks
Matplotlib half black and half white circle
I found the same question here. It is a hard one to find as search words are not common