Matplotlib: Multiple Histograms in One Plot [duplicate] - python

This question already has answers here:
How to plot in multiple subplots
(12 answers)
Closed 4 years ago.
I have several histograms that I want to include in a single figure. I know I can do this:
plt.title("Mondays")
plt.hist(mon["price"], bins=50, alpha=0.5, histtype='bar', ec='black')
plt.show()
But if I add another plt.hist(...) before calling plt.show(), matplotlib adds the second histogram on top of the first one. I'd like separate subplots for each of mon["price"], tues["price"], ..., sun["price"].
How would I go about that?

You can use subplots as in this example: matplotlob documentation 2 plots
plt.subplot(211) means: 2 rows, 1 column, 1:this is the 1st plot.
Here is an example with 4 plots: 2 rows and 2 columns:
4 plots

Related

Subplotting is reseting the first plot's labels in Pandas [duplicate]

This question already has answers here:
When should you use ax. in matplotlib?
(1 answer)
What is the difference between drawing plots using plot, axes or figure in matplotlib?
(2 answers)
Closed 5 months ago.
I am trying to subplot these 2 pandas bar plot but when I create a subplot the tags provided with xticks are deleted in the first subplot. I tried to plt.hold(True) but didn't solve the problem. What exactly is causing this error? You can find visualization of the problem and code below, thanks in advance.
fig, (ax1, ax2) = plt.subplots(1, 2)
#Get the first 10 common words by head
words_freq_head = pd.DataFrame(words_freq).head(10)
#Plot the first 10 common words
words_freq_head.plot.bar(ax=ax1)
#Set the label of common words on x axes
plt.xticks(np.arange(len(words_freq_head[0])), words_freq_head[0])
#Get the first 10 uncommon words by tail
words_freq_tail = pd.DataFrame(words_freq).tail(10)
#Plot the first 10 uncommon words
words_freq_tail.plot.bar(ax=ax2)
#Set the label of uncommon words on x axes
plt.xticks(np.arange(len(words_freq_tail[0])), words_freq_tail[0])

Matplotlib two axes - one legend [duplicate]

This question already has answers here:
How do I make a single legend for many subplots?
(10 answers)
How to put the legend outside the plot
(18 answers)
Closed 6 months ago.
I've created a plot with two axes and one legend containing every line regardless of its axis.
I want to use the parameter <loc="best"> which works for all lines in axis 1. I also want it to work with axis 2. Any ideas?
My code:
lns = self.ln1 + self.ln2 + self.ln3
labels = [l.get_label() for l in lns]
self.ax1.legend(lns, labels, loc="best")
As I assign one legend only to axis one, the loc parameter also only applies to axis one. How can I change this so I still only have one legend but the loc parameter works with all axes.

stripplot and box plot from dataframe in python [duplicate]

This question already has answers here:
seaborn boxplot and stripplot points aren't aligned over the x-axis by hue
(1 answer)
Seaborn boxplot + stripplot: duplicate legend
(2 answers)
How to do a boxplot with individual data points using seaborn
(1 answer)
How to overlay data points on seaborn figure-level boxplots
(2 answers)
Closed 7 months ago.
I'm trying to make a box plot and a strip plot from the following dataframe, but I think I'm not putting the arguments or maybe the df should be re-arranged but I don't know how.
the dataframe is here
I'm hopping to have a strip plot and a box for each color (column) in the same plot.
I'd be grateful if you could help

seaborn.lmplot with single axes labels [duplicate]

This question already has answers here:
Python Seaborn Facetgrid change xlabels
(2 answers)
Common xlabel/ylabel for matplotlib subplots
(8 answers)
How to add a shared x-label and y-label to a plot created with pandas plot
(4 answers)
One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?)
(1 answer)
How to set common axes labels for subplots
(9 answers)
Closed last year.
What I need should be straighforward but I couldn't find a solution. Say we draw the following seaborn.lmplot:
import seaborn as sns; sns.set_theme(color_codes=True)
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", col="day", hue="day",
data=tips, col_wrap=2, height=3)
I simply want to have a single label for the x-axis and a single label for the y-axis instead of two as currently.
In other words, that the word 'tip' be printed only one time on the centre left of the graph, and that the word 'total_bill' be printed only one time on the bottom centre of the graph.
How do we do this?
EDIT: there is a similar question here One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?) yet it is not elaborated and does not solve my issue.

Is there a way to associate for each plot the relative legend using plotly? [duplicate]

This question already has answers here:
Plotly legend next to each subplot, Python
(2 answers)
Closed 2 years ago.
I'm using plotly.subplots to generate a subplot of 2 plotly.express elements. The result is printed below. I would like to obtain 2 different legends, with the associated element for each plot.
Here is the code used for generated the plots:
fig1 =px.line(df, x=df.index, y='average')
fig1.add_scatter(x=df.index, y=df['ave_10'],mode='lines', name='MA 10',opacity=0.6)
fig1.add_scatter(x=df.index, y=df['ave_50'],mode='lines', name='MA 50',opacity=0.6)
# plot volume
fig2= px.line(df, x=df.index, y='Volume')
# combine the 2 graphs
fig = make_subplots(rows=2,cols=1,subplot_titles = ['Average values of {} with moving average at 10 and 50 lags'.format(code),'Volume'])
for data in fig1.data:
fig.add_trace((go.Scatter(x=data['x'],y=data['y'], name = data['name'])),row=1,col=1)
for data in fig2.data:
fig.add_trace((go.Scatter(x=data['x'],y=data['y'], name = data['name'])),row=2,col=1)
fig.show()
Plotly supports only one legend per graph. They recommend you use annotations instead. You can do better with matplotlib.

Categories

Resources