How to create a single legend for subplots [duplicate] - python

This question already has answers here:
How do I make a single legend for many subplots?
(10 answers)
Closed 6 months ago.
I am trying to replicate the following plot but with a different set of data:
My current plot has everything you see except the legend in the top right corner. I am having a hard time figuring out how I am supposed to add this in with my current code:
fig = plt.figure()
plt.subplot(3, 1, 1)
plt.title('Task Switches and Avg Task Switches by Timestep', fontsize=10)
plt.ylabel('Task Switches', fontsize=9)
plt.xlim(-35, timestep_num + 35)
plt.xticks(np.arange(0, timestep_num+1, 50), fontsize=-1, color='white')
plt.yticks(np.arange(0, 61, 20), fontsize=6)
plt.plot([stepsum_list[i][6] for i in range(len(stepsum_list))], color='royalblue',
linewidth=0.7, linestyle='', marker='.', markersize=1)
plt.plot([stepsum_list[i][6]/(i+1) for i in range(len(stepsum_list))], color='limegreen',
linewidth=0.6,)
plt.subplot(3, 1, 2)
plt.title('Task Demand per Timestep by Task', fontsize=10)
plt.ylabel('Task Demand', fontsize=9)
plt.xlim(-35, timestep_num + 35)
plt.xticks(np.arange(0, timestep_num+1, 50), fontsize=-1, color='white')
plt.yticks(np.arange(0, 6, 1), fontsize=6)
plt.plot([stepdem_list[i][1] for i in range(len(stepdem_list))], color='darkorange',
linewidth=0.7, linestyle='', marker='.', markersize=1)
plt.plot([stepdem_list[i][2] for i in range(len(stepdem_list))], color='yellowgreen',
linewidth=0.7, linestyle='', marker='.', markersize=1)
plt.plot([stepdem_list[i][3] for i in range(len(stepdem_list))], color='purple',
linewidth=0.7, linestyle='', marker='.', markersize=1)
plt.plot([stepdem_list[i][4] for i in range(len(stepdem_list))], color='blue', linewidth=0.7,
linestyle='', marker='.', markersize=1)
plt.subplot(3, 1, 3)
plt.title('Target and Tracker Movement',fontsize=10)
plt.ylabel('Movement', fontsize=9)
plt.xlabel('Timesteps', fontsize=9)
plt.xlim(-35, timestep_num + 35)
plt.xticks(np.arange(0, timestep_num+1, 50), fontsize=8)
plt.yticks(np.arange(-10, 11, 10), fontsize=6)
plt.plot([stepsum_list[i][4] for i in range(len(stepsum_list))], color='blue', linewidth=.5)
plt.plot([stepsum_list[i][2] for i in range(len(stepsum_list))], color='red', linewidth=.5)
fig.align_labels()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.4, hspace=0.4)
plt.savefig('prog02_output.png')
plt.show
I apologize for all of the repetitive code, I'm brand new to Python and this is my first time making a plot so I don't know all of the tricks just yet. I have found the function figlegend(), but I'm confused if this is what I am going to want to use, and if so how the parameters are working. Placing the legend in the correct spot (aligned with the top subplot) is also something I am trying to do, but can't seem to figure out.
I'm not asking anyone to write any code or rewrite what I have. Just for someone to point me in the right direction, whether that be explaining a function and what parameters it can take, or what might need to be changed in my current code to use figlegend().

The way I plot legends with my plots in Matplotlib is via the Axes.legend() function, shown below:
The source code is
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1,2],[2,1,0], c='r', label='Plot 1')
ax.plot([0,1,2],[0,1,2], c='b', label='Plot 2')
ax.legend()
plt.show()
After you add labels to each of the data traces in your plot via the label keyword argument, then you can add a legend to the figure with
plt.gca().legend()

If your try to put more 3 variables, you can do this:
lns1 = plt.plot(x,y)
lns2 = plt.plot(x2,y)
lns3 = plt.plot(x3,y)
lns = lns1+lns2+lns3+lns4
labs = [l.get_label() for l in lns]
plt.figure()
When you go to plot, use this:
ax.legend(lns,labs,loc='lower center')

Related

How can I implement plt.subplot correctly so my graphs can be side by side?

I am outputting two scatter plots, but I want both graphs to be next to each other side by side.
When I use plt.subplots, my ax1 and ax2 aren't being recognized? How can I make the bottom two scatterplots next to each other? Whenever I use plt.subplots it just creates empty graphs.
# Scatter plots.
ax1 = df_Baker.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(10, 7))
df_Muriel.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(10, 7), ax=ax1)
df_Tanner.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(10, 7), ax=ax1)
# regression lines
plt.plot(df_Baker.HS_GPA, Baker_fit[0] * df_Baker.HS_GPA + Baker_fit[1], color='darkblue', linewidth=2)
plt.plot(df_Tanner.HS_GPA, Tanner_fit[0] * df_Tanner.HS_GPA + Tanner_fit[1], color='deeppink', linewidth=2)
plt.plot(df_Muriel.HS_GPA, Muriel_fit[0] * df_Muriel.HS_GPA + Muriel_fit[1], color='deeppink', linewidth=2)
plt.legend(labels=['_h', '_hii', '_', '10 - 20','1 - 5'], title='Legend Test')
plt.title('BIO: Basic Concepts', size=24)
plt.xlabel('High school gpa', size=18)
plt.ylabel('cousre Grade', size=18);
#-----------------------------------------------------------------------------
# Scatter plots.
ax2 = df_Baker.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(6, 3))
df_Muriel.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(6, 3), ax=ax2)
df_Tanner.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(6, 3), ax=ax2)
# regression lines
plt.plot(df_Baker.HS_GPA, Baker_fit[0] * df_Baker.HS_GPA + Baker_fit[1], color='black', linewidth=2)
plt.plot(df_Tanner.HS_GPA, Tanner_fit[0] * df_Tanner.HS_GPA + Tanner_fit[1], color='black', linewidth=2)
plt.plot(df_Muriel.HS_GPA, Muriel_fit[0] * df_Muriel.HS_GPA + Muriel_fit[1], color='black', linewidth=2)
plt.legend(labels=['_h', '_hii', '_', '10 - 20','1 - 5'], title='Legend Test')
plt.title('BIO: Basic Concepts', size=24)
plt.xlabel('High school gpa', size=18)
plt.ylabel('cousre Grade', size=18);
Output graphs so far
In this line:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3))
your create the two axes objects and attach the names ax1 and ax2 to them.
Later, in
ax1 = df_Baker.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(10, 7))
(and similarely in the line for ax2), your create new axis objects, and assign the names ax1 and ax2 to them.
I seems that this is not what you want. Rather, I guess you want to use the previously generated axes objects in the calls to df.Bakker.plot(). You can achieve this by using the ax= keyword:
df_Baker.plot(kind='scatter', x='HS_GPA', y='Course_Grade', color='black', alpha=0.5, figsize=(10, 7), ax=ax1)
You will also have to change the plt.plot(...) calls to ax1.plot(...) or ax2.plot(...), and similar for the functions plt.xlabel, plt.ylabel, plt.legend.
I would suggest to read the Blog post https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/ on the difference between the Pyplot vs. Object Oriented Interface to Matplotlib, and you can also have a look at the examples referenced in https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html

X-axis minor gridlines still not showing even after trying all solutions

My x-axis minor gridlines are not showing, this is my code
ax = plt.gca()
ax.minorticks_on()
plt.semilogx(data_x1,data_y1,"red")
plt.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
plt.xlabel("frequency(Hz)")
plt.ylabel("Iramp(dB)")
plt.show()
enter image description here
Either I'm not sure of what you want, or your code is actually working correctly. The minor grid lines are those between the powers of 10. I made a little example to show a comparison of your plot with the minor grid lines on and off.
import numpy as np
import matplotlib.pyplot as plt
data_x1 = np.linspace(0,2,10)
data_x2 = np.linspace(0,4,10)
data_y1 = np.random.rand(10)
data_y2 = np.random.rand(10)
fig, axall =plt.subplots(1,2, figsize=(10,5))
# your code with some changes
ax = axall[0]
ax.minorticks_on()
ax.semilogx(data_x1,data_y1,"red")
ax.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
ax.set_xlabel("frequency(Hz)")
ax.set_ylabel("Iramp(dB)")
# code to make the plot on the right.
ax = axall[1]
ax.minorticks_on()
ax.semilogx(data_x1,data_y1,"red")
ax.semilogx(data_x2,data_y2,"blue")
ax.grid(b=True, which='major',axis="both", color='k', linestyle='-', linewidth=0.5)
# ax.grid(b=True, which='minor',axis="both", color='k', linestyle='-', linewidth=0.2)
ax.set_xlabel("frequency(Hz)")
ax.set_ylabel("Iramp(dB)")
plt.show()
Note how I commented out your minor grid lines.

No handles with labels found to put in legend

I'm trying to create a parallelogram in PyPlot. I'm not up to drawing the parallelogram--first I'm putting in the vector arrows--using the following code:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.axis([-5,5,-5,5])
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid()
plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u')
plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v')
plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v')
plt.legend()
This returns the following error:
No handles with labels found to put in legend.
I'm not sure why, because, based on the documentation for plt.arrow(), label is an acceptable kwarg, and plt.legend() should ostensibly be reading that. The rest of the figure draws fine; it's just missing the legend.
It might be late but for anyone with the same issue the solution is using the method legend() for the corresponding ax not as for plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
plt.axis([-5,5,-5,5])
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.grid()
plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u')
plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v')
plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v')
ax.legend()
You can explicitly define the elements in the legend.
For full control of which artists have a legend entry, it is possible to pass an iterable of legend artists followed by an iterable of legend labels respectively. Reference
Example:
arr1 = plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True)
arr2 = plt.arrow(0,0, 1,3, head_width=0.2, color='g', length_includes_head=True)
arr3 = plt.arrow(0,0, 4,4, head_width=0.2, color='b', length_includes_head=True)
plt.xlim(0,5)
plt.ylim(0,5)
plt.legend([arr1, arr2, arr3], ['u','v','u+v'])
The error is thrown because you haven't specified the label text
Either do something like this
plt.hist([x01, x02,x03], color=["lightcoral","lightskyblue","slategrey"], stacked=True,
label=['Supressed','Active','Resolved'])
plt.legend()
Or
Do not use plt.legend() if you haven't specified the label text as in the following WRONG example:
plt.hist([x01])
plt.legend()
The above will throw the same error, so either remove legend function or provide what it needs -> label.
Side note: Here x01 is just a list of number for which I am creating a histogram, in the first example they are three list of numbers to create stacked bar chart
The bottom line is this error is thrown because of not specifying legend text and calling/initializing a legend
I had this error when using labels which started with an underscore
plt.plot(x, y, label = '_bad_name')
Removing the front underscore from the labels solved the issue
Assuming you have 2 plots ax and ax2, we can:
get the labels from each y-axis via ax.get_label()
.legend allows an array to be ingested
fig.legend([ax.get_ylabel(), ax2.get_ylabel()], loc='upper right')
I had this same issue and solved it with an understanding that .legend() function has to be after all the instructions that deal with the label attribute. This includes both plt and ax.
So moving ax.legend(*) as the last command.
I hope this helps you too.
Change
ax.plot(-trip_df.stop_lat, -trip_df.stop_lon, label = trip_id)
plt.legend()
to
ax.plot(-trip_df.stop_lat, -trip_df.stop_lon, label = trip_id)
ax.legend()
plt.legend()
I face similar problem like No handles with labels found to put in legend.
First My code look like
figure, axis = pyplot.subplots(nrows=1,ncols=2, figsize=(15, 6), tight_layout=True)
axis[0].legend(title='Country', title_fontsize = 12) #this line
axis[0].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name'])
axis[1].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name')
pyplot.show()
Then I changed to
figure, axis = pyplot.subplots(nrows=1,ncols=2, figsize=(15, 6), tight_layout=True)
axis[0].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name'])
axis[0].legend(title='Country', title_fontsize = 12) # this line
axis[1].pie(x=piechart_result['value_eur'],labels=piechart_result['short_name')
pyplot.show()
this work for me in colab notebook

Set minor ticks in all log-log subplots

I have a figure with two subplots in log-log scale. I would like to plot the minor ticks as well. Even though I have applied different solutions from Stack Overflow, my figure does not look as I want.
One of the solutions I have modified comes from ImportanceOfBeingErnest and the code looks like this:
fig, ((ax1, ax2)) = plt.subplots(1, 2, figsize=(8, 5), sharey=True)
# First plot
ax1.loglog(PLOT1['X'], PLOT1['Y'], 'o',
markerfacecolor='red', markeredgecolor='red', markeredgewidth=1,
markersize=1.5, alpha=0.2)
ax1.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax1.set_xscale("log"); ax1.set_yscale("log")
ax1.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax1.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25)
ax1.xaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.xaxis.set_minor_locator(locmin)
ax1.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25)
ax1.yaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.yaxis.set_minor_locator(locmin)
ax1.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
ax1.set_xlabel('X values', fontsize=10, fontweight='bold')
ax1.set_ylabel('Y values', fontsize=10, fontweight='bold')
# Plot 2
ax2.loglog(PLOT2['X'], PLOT2['Y'], 'o',
markerfacecolor='blue', markeredgecolor='blue', markeredgewidth=1,
markersize=1.5, alpha=0.2)
ax2.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax2.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax2.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25)
ax2.xaxis.set_major_locator(locmaj)
ax2.yaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax2.xaxis.set_minor_locator(locmin)
ax2.yaxis.set_minor_locator(locmin)
ax2.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
ax2.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
ax2.set_xlabel('X values', fontsize=10, fontweight='bold')
ax2.set_ylabel('Y values', fontsize=10, fontweight='bold')
ax2.minorticks_on()
plt.show()
The plot I get is the following. As you can see, the minor ticks only appear on the x-axis from ax1.
How can I set the minor ticks in both subplots and both axis (x and y)?
Thank you so much.

Assign figure to correct subplot

I have a set of subplot's that display different information. For the example below, I can assign the scatter plot to the designated subplot but the two distplot occupy the last subplot created.
import matplotlib.pyplot as plt
import seaborn as sns
x = [1,4,5,6,7,8]
x2 = [3,4,8,2,8,8]
y = [1,2,4,8,1,9]
def L_plot(ax, fontsize=12):
ax.set_xlabel('x-label', fontsize=8)
ax.set_ylabel('y-label', fontsize=8)
ax.set_title('L', fontsize=10)
ax.grid(False)
ax.scatter(x, y)
def E_plot(ax2,pid, fontsize=12):
ax2.set_xlabel('x-label', fontsize=8)
ax2.set_ylabel('y-label', fontsize=8)
ax2.set_title('E', fontsize=10)
ax2.grid(False)
ax2 = sns.distplot(x, kde=False, norm_hist=True, color='b', bins = 10)
def D_plot(ax,pid, fontsize=12):
ax.set_xlabel('x-label', fontsize=8)
ax.set_ylabel('y-label', fontsize=8)
ax.set_title('D', fontsize=10)
ax.grid(False)
ax = sns.distplot(x2, kde=False, norm_hist=True, color='b', bins = 10)
ax1 = plt.subplot2grid((3,1), (0, 0))
ax2 = plt.subplot2grid((3,1), (1, 0))
ax3 = plt.subplot2grid((3,1), (2, 0))
L_plot(ax1,1)
E_plot(ax2,1)
D_plot(ax3,1)
plt.tight_layout()
plt.show()
I'm trying to assign E_plot to the subplot in the second row but both distplot's are located in the last subplot created.
I'm not sure if the seaboard packages can't be assigned or I'm not correctly assigning it?
The call signature for distplot is:
seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False,
fit=None, hist_kws=None, kde_kws=None, rug_kws=None,
fit_kws=None, color=None, vertical=False,
norm_hist=False, axlabel=None, label=None,
ax=None)
Notice the last option. If you don't tell it which Axes object to use, it'll use the one returned by pyplot.gca() (gca = "get current Axes").
So you need to do, e.g.,
sns.distplot(x2, kde=False, norm_hist=True, color='b', bins=10, ax=ax2)

Categories

Resources