Use subplots for pie char and bar char pandas [duplicate] - python

This question already has answers here:
How to plot multiple dataframes in subplots
(10 answers)
Plotting Pandas into subplots
(1 answer)
Closed 1 year ago.
I'm trying to show two graphs next to each other. pie char and bar char.
my code is:
import matplotlib.pyplot as plt
fig , ax = plt.subplots(nrows = 1, ncols = 2)
df['column'].value_counts().plot.pie()
df['column'].value_counts().plot.bar()
plt.show()
this is the output:
can someone help me please?

Pass the subplots to the plot commands:
fig , ax = plt.subplots(nrows = 1, ncols = 2)
df['column'].value_counts().plot.pie(ax=ax[0])
df['column'].value_counts().plot.bar(ax=ax[1])
plt.show()

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

Shaded area either side of mean on line graph - matplotlib, seaborn - Python [duplicate]

This question already has answers here:
Why am I getting a line shadow in a seaborn line plot?
(2 answers)
Seaborn lineplot using median instead of mean
(2 answers)
Closed 10 months ago.
`def comparison_visuals(df_new):
matplotlib.rc_file_defaults()
ax1 = sns.set_style(style=None, rc=None )
fig, ax1 = plt.subplots(figsize=(12,6))
sns.lineplot(data = df_new, x='Date', y=
(df_new['Transfer_fee'])/1000000, marker='o', sort = False,
ax=ax1)
ax2 = ax1.twinx()
from matplotlib.ticker import FormatStrFormatter
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
sns.lineplot(data = df_new, x='Date', y='Inflation', alpha=0.5,
ax=ax2)
from matplotlib.ticker import FormatStrFormatter
ax2.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))
comparison_visuals(df_new)'
(Edited to paste code above)
Can anyone tell me what the shaded area represents on my line graph (see screenshot)? The middle line represents the mean. I haven't specifically added it. I would like to know first what it is and secondly how to remove it (I might chose to keep it if I find out what it represents and it adds value to my visualisation).
Any related answers I've come across don't del with this directly. Thansk in advance.
Screenshot of line graph

How to add displot to sub-histogram using matplotlib [duplicate]

This question already has answers here:
Add density curve on the histogram
(2 answers)
Closed 8 months ago.
I have simplified code like this:
fig, axs = plt.subplots(1, 2)
axs[0].hist(x)
axs[1].hist(y)
and I need to add density curve to each plot. Anyone know reasonable simple way to do this? It could be using seaborn. Kindly help.
Funtion seaborn.displot() does not working in subplots.
You could use seaborn.histplot and pass kde parameter:
fig, axs = plt.subplots(1, 2)
sns.histplot(x, ax = axs[0], kde = True)
sns.histplot(y, ax = axs[1], kde = True)

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!

python: changing the size of ax.matshow in matplotlib [duplicate]

This question already has answers here:
Matplotlib: Getting subplots to fill figure
(3 answers)
Closed 5 years ago.
I was trying to plot a heatmap using matplotlib similar to the heatmap of plotly. I am able to get the output by the size of the matshow figure is very small. The following is the figure
Is it possible to get the following figure:
The following is my code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
z = []
for _ in range(7):
new_row = []
for __ in range(180):
new_row.append(np.random.poisson())
z.append(list(new_row))
df1 = pd.DataFrame(np.array(z), columns=range(len(z[0])))
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
cax = ax.matshow(df1, interpolation='nearest', cmap='coolwarm')
fig.colorbar(cax)
ax.set_xticklabels([''] + list(df1.columns))
ax.set_yticklabels([''] + list(df1.index))
plt.show()
Kindly help.
You may want to use
ax.matshow(... , aspect="auto")
to remove the restriction of equal aspect on imshow or matshow.

Categories

Resources