multiple pie chart for each row pandas - pandas

I would like to create multiple pie chart for each continent to show the alcohol serving with percentage on it.
Thank you

You can use DataFrame.plot.pie with transpose dataframe by T:
df = pd.DataFrame({'beer':[1,2,3],
'spirit':[4,5,6],
'wine':[7,8,9]}, index=['Africa','Asia','Europe'])
print (df)
beer spirit wine
Africa 1 4 7
Asia 2 5 8
Europe 3 6 9
df.T.plot.pie(subplots=True, figsize=(10, 3))

here is the code, I found this kind more flexible
from matplotlib import pyplot as plt
import pandas as pd
df = pd.DataFrame({'beer':[1,2,3],
'spirit':[4,5,6],
'wine':[7,8,9]}, index=['Africa','Asia','Europe'])
df= df.div(df.sum(axis=1), axis=0)
fig, axs = plt.subplots(nrows=df.index.size, ncols=1, figsize=(7,7))
fig.subplots_adjust(hspace=0.5, wspace=0.05)
for row in range(df.index.size + 1):
fig.add_subplot(axs[row] )
plt.pie(df.loc[df.index[row],:], labels=df.columns)
plt.axis('off')

Related

How to plot in pandas after groupby function

import pandas as pd
df = pd.read_excel(some data)
df2 = df.groupby(['Country', "Year"]).sum()
It looks like this:
Sales COGS Profit Month Number
Country Year
Canada 2013 3000
Canada 2014 3500
Other countries... other data
df3 = df2[[' Sales']]
I can plot it like this with the code:
df3.plot(kind="bar")
And it produces a chart
But I want to turn it into a line chart but my result from a simple plot is:
Stuck as to what one-liner will produce a chart that segments time on the x-axis but plots sales on y-axis with lines for different countries.
You have to stack Country column:
import matplotlib.pyplot as plt
df2 = df.groupby(['Country', 'Year'])['Sales'].sum().unstack('Country')
# Or df2.plot(title='Sales').set_xticks(df2.index)
ax = df2.plot(title='Sales')
ax.set_xticks(df2.index)
plt.show()
Output:

Graph plotting in pandas and seaborn

I have the table with 5 columns with 8000 rows:
Market DeliveryWindowID #Orders #UniqueShoppersAvailable #UniqueShoppersFulfilled
NY 296 2 2 5
MA 365 3 4 8
How do I plot a graph in pandas or seaborn that will show the #Order, #UniqueShoppersAvailable, #UniqueShoppersFulfilled v/s the market and delivery window?
Using Seaborn, reshape your dataframe with melt first:
df_chart = df.melt(['Market','DeliveryWindowID'])
sns.barplot('Market', 'value',hue='variable', data=df_chart)
Output:
One way is to set Market as index forcing it onto the x axis and do a bar graph if you wanted a quick visualization. This can be stacked or not.
Not Stacked
import matplotlib .pyplot as plt
df.drop(columns=['DeliveryWindowID']).set_index(df.Market).plot(kind='bar')
Stacked
df.drop(columns=['DeliveryWindowID']).set_index(df.Market).plot(kind='bar', stacked=True)

How to plot in pandas - Different x and different y axis in a same plot

I want to plot different values of x and y-axis from different CSVs into a simple plot.
csv1:
Time Buff
1 5
2 10
3 15
csv2:
Time1 Buff1
2 3
4 6
5 9
I have 5 different CSVs. I tried plotting to concatenate the dataframes into a single frame and plot it. But I was able to plot with only one x-axis:
df = pd.read_csv('csv1.txt)
df1 = pd.read_csv('csv2.txt)
join = pd.concat([df, df1], axis=1)
join.plot(x='Time', y=['Buff', 'Buff1'], kind='line')
join.plot(x='Time', y='Buff', x='Time1', y='Buff1') #doesn't work
I end up getting a plot with reference with only one x-axis (csv1). But how to plot both x and y column from the CSVs into the same plot?
you can plot two dataframes in the same axis if you specify that axis with ax=. Notice that I created the figure and axis using subplots before i plotted either of the dataframes.
import pandas as pd
import matplotlib.pyplot as plt
f,ax = plt.subplots()
df = pd.DataFrame({'Time':[1,2,3],'Buff':[5,4,3]})
df1 = pd.DataFrame({'Time1':[2,3,4],'Buff1':[5,7,8]})
df.plot(x='Time',y='Buff',ax=ax)
df1.plot(x='Time1',y='Buff1',ax=ax)

Plotting period series in matplotlib pyplot

I'm trying to plot timeseries revenue data by quarter with matplotlib.pyplot but keep getting an error. Below is my code and the errors The desired behavior is to plot the revenue data by quarter using matplotlib. When I try to do this, I get:
TypeError: Axis must havefreqset to convert to Periods
Is it because timeseries dates expressed as periods cannot be plotted in matplotlib? Below is my code.
def parser(x):
return pd.to_datetime(x, format='%m%Y')
tot = pd.read_table('C:/Desktop/data.txt', parse_dates=[2], index_col=[2], date_parser=parser)
tot = tot.dropna()
tot = tot.to_period('Q').reset_index().groupby(['origin', 'date'], as_index=False).agg(sum)
tot.head()
origin date rev
0 KY 2016Q2 1783.16
1 TN 2014Q1 32128.36
2 TN 2014Q2 16801.40
3 TN 2014Q3 33863.39
4 KY 2014Q4 103973.66
plt.plot(tot.date, tot.rev)
If you want to use matplotlib, the following code should give you the desired plot:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'origin': ['KY','TN','TN','TN','KY'],
'date': ['2016Q2','2014Q1','2014Q2','2014Q3','2014Q4'],
'rev': [1783.16, 32128.36, 16801.40, 33863.39, 103973.66]})
x = np.arange(0,len(df),1)
fig, ax = plt.subplots(1,1)
ax.plot(x,df['rev'])
ax.set_xticks(x)
ax.set_xticklabels(df['date'])
plt.show()
You could use the xticks command and represent the data with a bar chart with the following code:
plt.bar(range(len(df.rev)), df.rev, align='center')
plt.xticks(range(len(df.rev)), df.date, size='small')
It seems like bug.
For me works DataFrame.plot:
ooc.plot(x='date', y='rev')

Showing one label on pie chart pandas

Is there a way of showing just one set of label? At the moment it is looking very messy and I would like to have one set of label please. I did label=None and it turned off all the labels.
Thanks
I think you need a bit change How to make MxN piechart plots with one legend and removed y-axis titles in Matplotlib:
df = pd.DataFrame({'beer':[1,2,3],
'spirit':[4,5,6],
'wine':[7,8,9]}, index=['Africa','Asia','Europe'])
print (df)
beer spirit wine
Africa 1 4 7
Asia 2 5 8
Europe 3 6 9
fig, axes = plt.subplots(1,3, figsize=(10,3))
for ax, idx in zip(axes, df.index):
ax.pie(df.loc[idx], labels=df.columns, autopct='%.2f')
ax.set(ylabel='', title=idx, aspect='equal')
axes[0].legend(bbox_to_anchor=(0, 0.5))
plt.show()