I have a xarray dataset clip_ds and have visualised the data array using plot. Now, I want to add a country boundary using Basemap's drawcountries(). Apparently, there is something wrong with the extent I am using in basemap (I guess), but both the country border and the data plot won't show up together. I have tried interchanging the position of clip_ds.pr[0].plot() before and after I create basemap, and it gives me two different results as shown below:
Xarray PLOT BEFORE BASEMAP (Note that the colorbar from xarray plot is still there)
Xarray PLOT AFTER BASEMAP (Notice difference from 3, height of the plot shrinks here and the tick labels disappears, probably overlapped by basemap.)
Xarray plot only
Loading seperate shapefile using map.readshapefile also gives same kind of problem. I know there might be a way around this using cartopy, but I like the Basemap functionalities and would like to know if there is any solution to this.
map=Basemap(projection='merc',
resolution='l',
llcrnrlon=clip_ds.lon[0],
llcrnrlat=clip_ds.lat[0],
urcrnrlon=clip_ds.lon[-1],
urcrnrlat=clip_ds.lat[-1])
map.drawcountries()
clip_ds.pr[0].plot()
plt.show()
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 have a set of data, 5 columns: (x,y,z,Temperature, Pressure), is not a regular mesh, is data from well logging. I would like to make a 3d grid in order to make some vertical cut. How can I do it?
Your question isn't very clear about what you want. If it is a graph packages like seaborn and matplotlib can make 3D graphs that can display heatmap information.
I want to plot boxplots on top of the scattered points like this.
I know I have to bin the data into intervals first but I couldn't find the function that does all of this. Sample x and y data are saved here as .npy.
I would look into using matplotlib. Boxes can be drawn as such:
https://matplotlib.org/gallery/pyplots/boxplot_demo_pyplot.html?highlight=boxplot
and scatter plots can also be drawn as such: https://matplotlib.org/gallery/lines_bars_and_markers/scatter_demo2.html?highlight=scatter
There is a search functionality on their site, along with plenty of documentation on how to utilize their library.
As for your specific question, you can specify zorder when drawing many of the things in matplotlib, and you could use that to define your boxplots to be on top. I believe if no zorder is defined that it draws items in the order they are encountered in your program (so you could draw scatter plots and then box plots and they should appear correctly as in your diagram above!
I have a Matplotlib scatter plot with 10,000+ points that I plan to insert as a figure in a LaTeX document for publication.
I would like the plot points to be raster graphics (e.g. PNG) because vector graphics with that many points often causes problems for PDF readers. I would like the ticks and axes labels to be vector graphics so I don't have to worry about resolution issues for the text and lines.
Is there a simple way to get matplotlib to make parts of the plot raster graphics while keeping the axes/ticks vector graphics?
My best guess so far is to do some sort of pre-render to PNG then imshow the resulting image with appropriate axes bounds before saving to PDF.
Add rasterized=True to the call to plt.scatter
See the docs here
You can control the dpi of the rasterized parts of the figure by setting dpi=300 (for example) in the call to plt.figure
Here's the result of scatter plot using Matplotlib
And now here's the result of calling scatter plot using Pandas
Is there bug in Pandas scatter function or is it supposed to work like this?
I think the grey area you see is the boundary of each point. Use the argument edgecolors='none' or edgecolors='black' to get the same result as you get with matplotlib (see also http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)