How to add legend in plot with subplots [duplicate] - python

This question already has answers here:
matplotlib scatterplot with legend
(2 answers)
Matplotlib scatter plot legend
(5 answers)
How to put the legend outside the plot
(18 answers)
Closed 5 years ago.
I have this plot
I want to put legend with color codes horizontally below the plot. Also it will be great if I can chose the color in both the plots and in the legend. Right now it is generated as
fix,axes=plt.subplots(nrows=5,ncols=4, figsize=(20,20))
plots=[]
for i in range(0,5):
for j in range(0,4):
x = list(bdf.groupby('condition').mean(). [bdf.columns[detection_idx]])
y1 = list(df.groupby('condition').mean() [df.columns[aesthetic_idx]])
plots.append(axes[i][j].scatter(x,y1, c = range(0, len(conditions))))
s = list(scenes.values())[i*4+j]
axes[i][j].title.set_text(s)
axes[i][j].set_xlabel('Detection accuracy')
axes[i][j].set_ylabel('Visual aesthetics')
aesthetic_idx+=4
detection_idx+=1
plt.figlegend(tuple(plots), tuple(conditions), loc='lower left', bbox_to_anchor=(0.0, -0.1), ncol=len(conditions))

Related

How to set xticks for individual axes in seaborn catplot [duplicate]

This question already has answers here:
Set xticklabels for all grids, for a plot created with seaborn catplot that uses col_wrap [duplicate]
(1 answer)
How to rotate xticklabels in a seaborn catplot
(2 answers)
subplotting with catplot
(1 answer)
How to remove xticks from a catplot when there's no associated data
(1 answer)
Closed 10 months ago.
I have below code with axes in rows but only the last axes shows the xticks while I want to show the xticks on on each Axes. Please someone help me with this and also guide me how to individually control all the elements of axes.
g = sns.catplot(data = df_full[df_full['Team'].isin(Test_playing_list)], x = 'Match_Year', #palette = sns.color_palette('Paired', 7),
y = 'Win_percent',kind = "bar", height=3, aspect=3, linewidth = 2, row = 'Team', orient="v", facet_kws={'sharey':False, 'sharex':False})
plt.xticks(rotation = 90)
#plt.grid()
plt.show

Make x axis labels to be wrapped in seaborn plots for python [duplicate]

This question already has answers here:
Wrapping long y labels in matplotlib tight layout using setp
(2 answers)
How to wrap xtick labels in matplotlib? [duplicate]
(1 answer)
split tick labels or wrap tick labels [duplicate]
(1 answer)
Closed 1 year ago.
I am plotting some variables but the data labels are a bit long so they are overlapping. I would like them to be displayed in multiple lines. The code I am using is this:
lista = list(df['col'].unique())
sns.countplot(data = df,x = 'col').set_xticklabels( lista,wrap=True)
sns.despine()
plt.show()
Seaborn plot

Matplotlib not plotting several points on horizontal barplot? [duplicate]

This question already has answers here:
Plot negative values on a log scale
(2 answers)
Negative axis in a log plot
(1 answer)
Plot logarithmic axes
(6 answers)
Closed 1 year ago.
Matplotlib appears to not be plotting several points despite me changing the x-limit based on the inputted numbers I am trying to graph. (x-limit because this is a horizontal barplot).
Here is the code:
low = min(df_grouped_underlying['Currency Exposure'].astype(float))
high = max(df_grouped_underlying['Currency Exposure'].astype(float))
fig = plt.figure()
ax = fig.add_subplot()
ax.set_xlim([math.ceil(low-0.5*(high-low)), math.ceil(high+0.5*(high-low))])
bar_1 = ax.barh(df_grouped_underlying.index, df_grouped_underlying['Currency Exposure'].astype(float), label='Currency Exposure')
ax.set(title = 'Currency_Exposure_Underlying_Position',
ylabel = 'Underlying Currency',
xlabel = 'Exposure')
plt.legend(loc="upper right")
plt.savefig('agg_exposure.png', bbox_inches='tight')
plt.show()
Here is the output:
Output of the plot

Python matplotlib subplot clumsy datetime xticks issue [duplicate]

This question already has answers here:
Rotating axis text for each subplot
(3 answers)
Date ticks and rotation in matplotlib
(6 answers)
Aligning rotated xticklabels with their respective xticks
(5 answers)
Closed 2 years ago.
I have subplots with shared x,y axis. The x-axis is datetime stamp. Everything looks fine. The problem is x-axis looks clumsy.
My code:
fig, ax = plt.subplots(nrows=rows, ncols=cols,sharey=True,sharex=True)
fig.subplots_adjust(hspace=0.025, wspace=0.05)
for i in range(rows):
for j in range(cols):
#### plot data goes here.
ax[i,j].autofmt_xdate()
ax[i,j].xaxis.set_tick_params(rotation = 30)
fig.subplots_adjust(right=0.95)
plt.show()
Present output: The x-axis labels look clumsy, hard to read.

How to show colorbar on each individual matshow subplot [duplicate]

This question already has answers here:
matplotlib colorbar in each subplot
(5 answers)
Closed 3 years ago.
I am creating a (10,7) subplot of multiple different gridded fields. The following code is what is being currently used:
fig, axes = plt.subplots(nrows=10, ncols=7, figsize=(18, 16), dpi= 100,
facecolor='w', edgecolor='k')
titles = ['Z1','Z2','Z3','ZDR1','ZDR2','ZDR3','Dist']
for i in range(0,10):
z = 1*10+i
for j in range(0,7):
aa = axes[i,j].matshow(alldata_sim[z,:,:,j], cmap='jet')
fig.colorbar(aa)
axes[0,j].set_title(titles[j])
axes[i,j].get_xaxis().set_visible(False)
axes[i,j].get_yaxis().set_ticks([])
axes[i,0].set_ylabel(allgauge_sim[z])
Which produces the following figure:
Figure1
The question is: how do I get the colorbars to be on the right-hand side of each respective individual subplot?
maybe try changing
fig.colorbar(aa)
to
fig.colorbar(aa,ax=axes[i,j])
Hope it helps!

Categories

Resources