"set_xlabel" or "set_ylabel " is not supported in GeoAxesSubplot of cartopy v0.21.1 - cartopy

I used cartopy v0.21.1 to plot subplots, but "set_xlabel" or "set_ylabel " is not supported in GeoAxesSubplot.
Any recommendations for how to solve this or investigate further?
Thanks!
fig, axs = plt.subplots(nrows=3,ncols=3,subplot_kw={'projection': background.crs},figsize=(11,8))
axs=axs.flatten()
axs.set_ylabel('Isoprene')
How to set_xlabel or set_ylabel in GeoAxesSubplot

Related

How to avoid matplotlib to simplify my Y axis in figure?

the image of what I mean in my question
I'm using BMP280 to measure Temperature and Pressure using Raspberry.
I'm using matplotlib to make a graph, but the matplotlib simplify my Y axis bi adding +9.967e2.
is there any way to avoid matplotlib simplify my Y axis. Sorry I'm new to this so I don't know much.
I tried to search in google but I don't find anything. Maybe I'm using the wrong keyword as I don't know what should I search.
You can turn off the offset as shown in the examples here. For example, if you've made you plot with:
from matplotlib import pyplot as plt
plt.plot(x, y)
then you can turn off the offset with
ax = plt.gca() # get the axes object
# turn off the offset (on the y-axis only)
ax.ticklabel_format(axis="y", useOffset=False)
plt.show()
See the ticklabel_format docs for more info.

Is there a function in plotly that is equivalent to plt.axes('scaled') in matplotlib for the aspect ratio of a graph?

I want to plot some coordinates using Plotly express because it allows me a more interactive approach, but I can not find the way to control the scale in the axis in the way I can manage with matplotlib.pyplot in one single line
plt.axis("scaled")
Could you please share some suggestions? Thanks.
Here is the code using Plotly express:
fig = px.scatter(coordinates_utm, x='EASTING', y='NORTHING', title=name,
hover_name=coordinates_utm.index,
hover_data={'NORTHING':':.6f','EASTING': ':.6f'})
fig.add_trace(px.scatter(coordinates_utm_lineal, x='x', y='ylineal',color_discrete_sequence=['red']).data[0])
Here is the code using plt:
fig.show()
plt.figure()
plt.scatter(coordinates_utm_lineal.x,coordinates_utm_lineal.ylineal,s=2)
plt.scatter(coordinates_utm.EASTING,coordinates_utm.NORTHING, s=2)
plt.axis("scaled")
plt.show()
This is my current output
Sadly, you didn't provide a fully reproducible example, so I'm going to create my own.
Also, I'm not really familiar with plt.axis("scaled"), as I usually use plt.axis("equal"). Reading the documentation associated to plt.axis, they appear to be somewhat similar. See if the following answer can satisfy your needs.
import plotly.express as px
import numpy as np
t = np.linspace(0, 2*np.pi)
x = np.cos(t)
y = np.sin(t)
fig = px.scatter(x=x, y=y)
fig.layout.yaxis.scaleanchor="x"
fig.show()

Creating a grouped bar plot with Seaborn

I am trying to create a grouped bar graph using Seaborn but I am getting a bit lost in the weeds. I actually have it working but it does not feel like an elegant solution. Seaborn only seems to support clustered bar graphs when there is a binary option such as Male/Female. (https://seaborn.pydata.org/examples/grouped_barplot.html)
It does not feel right having to fall back onto matplotlib so much - using the subplots feels a bit dirty :). Is there a way of handling this completely in Seaborn?
Thanks,
Andrew
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rcParams
sns.set_theme(style="whitegrid")
rcParams.update({'figure.autolayout': True})
dataframe = pd.read_csv("https://raw.githubusercontent.com/mooperd/uk-towns/master/uk-towns-sample.csv")
dataframe = dataframe.groupby(['nuts_region']).agg({'elevation': ['mean', 'max', 'min'],
'nuts_region': 'size'}).reset_index()
dataframe.columns = list(map('_'.join, dataframe.columns.values))
# We need to melt our dataframe down into a long format.
tidy = dataframe.melt(id_vars='nuts_region_').rename(columns=str.title)
# Create a subplot. A Subplot makes it convenient to create common layouts of subplots.
# https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.subplots.html
fig, ax1 = plt.subplots(figsize=(6, 6))
# https://stackoverflow.com/questions/40877135/plotting-two-columns-of-dataframe-in-seaborn
g = sns.barplot(x='Nuts_Region_', y='Value', hue='Variable', data=tidy, ax=ax1)
plt.tight_layout()
plt.xticks(rotation=45, ha="right")
plt.show()
I'm not sure why you need seaborn. Your data is wide format, so pandas does it pretty well without the need for melting:
from matplotlib import rcParams
sns.set(style="whitegrid")
rcParams.update({'figure.autolayout': True})
fig, ax1 = plt.subplots(figsize=(12,6))
dataframe.plot.bar(x='nuts_region_', ax=ax1)
plt.tight_layout()
plt.xticks(rotation=45, ha="right")
plt.show()
Output:

Use different math font for different subplots in the same figure?

I am trying to use different math font sets for two axes in the same figure, with no success. I have searched this issue using google and I have read the matplotlib's official guide on how to use the math font. But I can not find ways to achieve this effect. My complete code is as follows:
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, (ax1, ax2) = plt.subplots(ncols=2)
mpl.rcParams['mathtext.fontset'] = 'cm' # use font "cm" for first axes
ax1.text(0.3, 0.5, r"$xyz$", fontsize=50)
ax1.set_title('before')
ax1.axis('off')
ax1.set_aspect('equal')
mpl.rcParams['mathtext.fontset'] = 'stixsans' # use font "stixsans" for second axes
ax2.text(0.3, 0.5, r"$xyz$", fontsize=50)
ax2.set_title('after')
ax2.axis('off')
ax2.set_aspect('equal')
plt.show()
The resulting figure shows that both the axes use the "stixsans" font, see picture here.
It seems that mpl.rcParams['mathtext.fontset'] = 'stixsans' in the later part has overruled the previous setting mpl.rcParams['mathtext.fontset'] = 'cm'. Any idea how to prevent this from happening and use "cm" and "stixsans" font for the two axes respectively?

Trying to add a 3d subplot to a matplotlib figure

So I'm trying to create a figure that presents a 3d plot from data points, along with the plots 3 projections in 3 other subplots. I can add the subplots for the projections with no problems, but when I try to place the 3 dimensional plot into the figure things backfire.
here's my code:
def plotAll(data):
fig = plt.figure()
plot_3d = fig.add_subplot(221)
ax = Axes3D(plot_3d)
for i,traj in enumerate(data.values()):
ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])
#plot_12v13 = fig.add_subplot(222)
#plot_projections(data,0,1)
#plot_13v14 = fig.add_subplot(223)
#plot_projections(data,1,2)
#plot_12v14 = fig.add_subplot(224)
#plot_projections(data,0,2)
#plt.plot()
which throws back:
'AxesSubplot' object has no attribute 'transFigure'
I'm using matplotlib 0.99.3, any help would be greatly appreciated, thanks!
I was searching for a way to create my 3D-plots with the nice fig, axes = plt.subplots(...) shortcut, but since I just browsed Matplotlib's mplot3d tutorial, I want to share a quote from the top of this site.
New in version 1.0.0: This approach is the preferred method of creating a 3D axes.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Note
Prior to version 1.0.0, the method of creating a 3D axes was different. For those using older versions of matplotlib, change ax = fig.add_subplot(111, projection='3d') to ax = Axes3D(fig).
So if you have to use the <1.0.0 version of Matplotlib, this should be taken into account.
If you would like to use plt.subplots instead of plt.subplot (see the difference here), then you can do something like this one:
import matplotlib.pyplot as plt
from matplotlib import cm # for a scatter plot
from mpl_toolkits.mplot3d import Axes3D
fig, ax = plt.subplots(1,2,figsize=(10,10),subplot_kw=dict(projection='3d'))
sc1 = ax[0].scatter(x,y,z, c = true, cmap=cm.jet)
ax[0].set_title('True solution')
sc2 = ax[1].scatter(x,y,z c = y_pred, cmap=cm.jet)
ax[1].set_title('Predicted Solution')
Well, I don't know how to set individual axes as 3D using plt.subplots. It would be helpful if someone could comment down.
The preferred way of creating an 3D axis is giving the projection keyword:
def plotAll(data):
fig = plt.figure()
ax = fig.add_subplot(221, projection='3d')
for i,traj in enumerate(data.values()):
ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])
plot_12v13 = fig.add_subplot(222)
plot_projections(data,0,1)
plot_13v14 = fig.add_subplot(223)
plot_projections(data,1,2)
plot_12v14 = fig.add_subplot(224)
plot_projections(data,0,2)
plt.plot()
Unfortunately, you didn't supply a working example with suitable data, so I couldn't test the code. Also, I would recommend updating to a newer version of matplotlib.