Below is the dataframe
Quantity UnitPrice CustomerID
Country
Netherlands 200128 6492.55 34190538.0
EIRE 142637 48447.19 110391745.0
Germany 117448 37666.00 120075093.0
France 110480 43031.99 107648864.0
Australia 83653 4054.75 15693002.0
How to plot a histogram with condition x axis as country(rotate 90) and Quanity on Y axis
df.hist(x,y)
You can try with this:
df.plot.bar(y='Quantity')
Here's the output:
Related
my dataframe is:
b1_df1_c.sort_values('score_s', ascending=False).head()
score score_s
CountryCode
Denmark 9.20 1.000000
Luxembourg 7.85 0.956297
Netherlands 6.52 0.913241
Czech Republic 5.69 0.886371
Belgium 4.96 0.862739
my barplot:
fig=plt.figure(figsize=(10,20))
plt.title("Tolerance (scaled)",family='Serif', weight='bold', size=20)
ax = sns.barplot(x='score_s', y=b1_df1_c.index, data=b1_df1_c)
with highest scores for: Austria, Average, Belgium, Bulgaria, Croatia, Cyprus
instead of Denmark, Luxembourg, Netherlands etc.
Does anybody have an idea why it sorts values so wrong?
Is the datatype of score_s actually numeric? Could be sorting by something other than the actual float value.
EDIT: I think Ben is correct. You are not saving the sorted version of the DataFrame.
I have a series as below:
Country
India 691904
China 659962
United Kingdom of Great Britain and Northern Ireland 551500
Philippines 511391
Pakistan 241600
United States of America 241122
Iran (Islamic Republic of) 175923
Sri Lanka 148358
Republic of Korea 142581
After i plotted a horizontal bar graph, the graph x-axis was arranged from low to high
Q:
1) How can I arrange the y-axis from High to Low?
2) Is there any easier way to colour all bar grey except for highest value (in this case India) without typing all the country name?
Thanks!
ax = top15.plot(kind='barh',
figsize=(20,10),
color = {'red':'India'})
ax.set_title('Immigration from 1980 - 2013')
ax.set_xlabel('Total Number')
Firstly use sort_values():
top15=top15.sort_values(ascending=True)
Finally:
color=['0.8','0.8','0.8','0.8','0.8','0.8','0.8','0.8','r']
#created a list of colors
ax = top15.plot(kind='barh',
figsize=(20,10),
color = color)
ax.set_title('Immigration from 1980 - 2013')
ax.set_xlabel('Total Number')
Output:
I am trying to plot a pie chart as a result of the function groupby on dataframe.
I am able to return the correct result of the groupby function but when o try to plot the pie chart its doesn't display all the result.
code:
tl = pd.unique(df['event_type'].tolist())
th = pd.unique(df['event_mohafaza'].tolist())
ct = df.groupby(['event_mohafaza','event_type']).aggregate('sum')
ct = ct.reset_index()
ct['labels'] = df['event_mohafaza'] + ' ' + df['event_type']
trace = go.Pie(labels=ct['labels'],
hoverinfo='label+percent',
values=ct['number_person'],
textposition='outside',
rotation=90)
layout = go.Layout(
title="Percentage of events",
font=dict(family='Arial', size=12, color='#909090'),
legend=dict(x=0.9, y=0.5)
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
fig.show()
result of the groupby function:
number_person
event_mohafaza event_type
loc1 swimming 9157
football 690
baseball 2292
loc2 swimming 10560
football 8987
baseball 70280
loc3 basketball 130
swimming 19395
football 5370
baseball 19078
loc4 swimming 9492
football 50
baseball 5279
loc5 swimming 4652
football 2215
baseball 3000
the plotted pie chart:
it doesn't display all the values it must divide the pie into 16 pieces where now its divided into 8 pieces
Try using:
values = ct['number_person'].value_counts()
I need to plot a histogram for the data below, country wise quantity sum.
Country Quantity
0 United Kingdom 4263829
1 Netherlands 200128
2 EIRE 142637
3 Germany 117448
4 France 110480
5 Australia 83653
6 Sweden 35637
7 Switzerland 30325
8 Spain 26824
9 Japan 25218
so far i have tried this but unable to specify the axis myself:
df.plot(x='Country', y='Quantity', kind='hist', bins=10)
Try a bar plot instead of a plot:
df.bar(x='Country', y='Quantity')
Try this :
import matplotlib.pyplot as plt
plt.bar(df['Country'],df['Quantity'])
plt.show()
I've this data of 2007 with population in Millions,GDP in Billions and index column is Country
continent year lifeExpectancy population gdpPerCapita GDP Billions
country
China Asia 2007 72.961 1318.6831 4959.11485 6539.50093
India Asia 2007 64.698 1110.39633 2452.21041 2722.92544
United States Americas 2007 78.242 301.139947 42951.6531 12934.4585
Indonesia Asia 2007 70.65 223.547 3540.65156 791.502035
Brazil Americas 2007 72.39 190.010647 9065.80083 1722.59868
Pakistan Asia 2007 65.483 169.270617 2605.94758 441.110355
Bangladesh Asia 2007 64.062 150.448339 1391.25379 209.311822
Nigeria Africa 2007 46.859 135.031164 2013.97731 271.9497
Japan Asia 2007 82.603 127.467972 31656.0681 4035.1348
Mexico Americas 2007 76.195 108.700891 11977.575 1301.97307
I am trying to plot a histogram as the following:
This was plotted using matplotlib (code below), and I want to get this with df.plot method.
The code for plotting with matplotlib:
x = data.plot(y=[3],kind = "bar")
data.plot(y = [3,5],kind = "bar",secondary_y = True,ax = ax,style='g:', figsize = (24, 6))
plt.show()
You could use df.plot() with the y axis columns you need in your plot and secondary_y argument as the second column
data[['population','gdpPerCapita']].plot(kind='bar', secondary_y='gdpPerCapita')
If you want to set the y labels for each side, then you have to get all the axes of the plot (in this case 2 y axis) and set the labels respectively.
ax1, ax2 = plt.gcf().get_axes()
ax1.set_ylabel('Population')
ax2.set_ylabel('GDP')
Output: