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

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])

Related

How to plot two charts on top of each other, sharing the same y axis, but having x axis mirror just for one of the charts in python [duplicate]

This question already has answers here:
matplotlib y-axis label on right side
(4 answers)
using pandas.DataFrame.melt() to plot data with seaborn
(1 answer)
seaborn multiple variables group bar plot
(1 answer)
Closed 8 months ago.
I have the chart below:
Almost everything is fine, except that I want to see the numbers close to the y axis on the right side of the chart.
They represent values that correspond to the labels on y axis as they are. I was thinking that maybe I could just do a mirror for the x axis for one of the plots, but not sure how to do it. Because if I do a mirror I end up doing it for everything.
My code is like that at the moment:
plt.figure(figsize=(12,10))
ax = sns.barplot(x='commission', y="market", hue="is_control", data= df_base.sort_values(by = dimension, ascending = False))
ax = sns.barplot(x="foods and goods", y="market", hue="is_control", data= df_base.sort_values(by = dimension, ascending = False))
plt.title("Treatment vs Control Group {}.".format(country), fontsize=14)
plt.xlabel('% of reduction', fontsize=10)
plt.ylabel('market', fontsize=10)
plt.tight_layout()
for i in ax.containers:
ax.bar_label(i,)

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.

Matplotlib - How to place figures next to each other? [duplicate]

This question already has answers here:
How to plot in multiple subplots
(12 answers)
Closed 3 years ago.
I have the following code:
def compare(f,a,b,c,d,n,points):
"""Plots 2 figures - one of the color map of f, and one of the color map of a rectangle [a,b] x [c,d], split
into n^2 subareas, using the list of points to estimate the color map"""
#fig, axes = plt.subplots(nrows=2, ncols=2)
q = plt.figure(1)
colorMapList(f,a,b,c,d,n,points)
#q.show()
p = plt.figure(2)
colorMap(f)
plt.show()
The functions colorMapList and colorMap both return ax.contourf(Y,X,z).
When I have the code the way I have it, the program outputs two diagrams, one below the other. How can I have it so that the diagrams are displayed horizontally next to each other?
Thanks!
If you want both graphs on a single figure then you can use plt.subplot(121) and plt.subplot(122). The first index is the number of rows and the second index is the number of cols. The third index is the position count of the figure layout, so if it was subplot(221) would be a 2x2 display of graphs and the 1 represents the graph in the upper left. Then, subplot(222) would be upper right, subplot(223) is bottom left, and subplot(224) is bottom right. This follow the sequence from top left to right for each row.
However, if you want to plot 2 different figures that are side-by-side then you can look at this solution.

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!

Matplotlib: Multiple Histograms in One Plot [duplicate]

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

Categories

Resources