Errorbars matplotliby python - python

i would like to know how i can add errorbars to my plot. Im attaching pic of code and plotenter image description here

Related

matplotlib figure image saved without figure title and axis

The following code gives the figure like the image below.
plt.subplot(1,1,1)
ax = sns.barplot(x=contr, y=X.columns)
ax.bar_label(ax.containers[0])
plt.title('Contribution')
plt.savefig('result_image.png')
plt.show()
What I can see in the jupyter notebook
However, the saved image ('result_image.png') has no titles or axis, but literally just figure box itself like the picture below
the real image file is like this
What I wanted is the plt image with title and axis.
=====
EDIT
The real problem was not the crop of the figure,
but the figure background being transparent.
(I didn't notice because the background of my photo application was dark)
I solved the problem with the code below.
plt.savefig('result_image.png', facecolor='white')
The real problem was not the crop of the figure, but the figure background being transparent.
(I didn't notice because the background of my photo application was dark)
I solved the problem with the codes below.
plt.savefig('result_image.png', facecolor='white')
or
plt.savefig('result_image.jpg')

Problem plotting a raster (GeoTIFF) on top of a basemap (Google Maps / Mapbox) with Python

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?

Zoom in a polar chart matplotlib

**Note: My code is at the end of the question.
I want to recreate this chart in matplotlib:
I have come a long way of making something similar but I am stuck in the last part, which is to zoom in the plot to remove all the white space. So basically, I want to zoom in to the red box:
Note that the grid will be removed on the actual chart. Displaying it now to explain what I would like done.
Is that possible? My code is here:
https://github.com/Curbal-Data-Labs/Matplotlib-Labs/blob/master/Polar%20charts/Polar%20bar%20chart.ipynb

Geopandas legend icon

I am plotting a map with geopandas and I a generate the following pic:
enter image description here
But I want to change the legend icon, is it possible to change for other format, like square, rectangle?

Can matplotlib commands shift the coordinates of a plot (raster image)?

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!

Categories