Home > Software design >  How to plot two bar diagram for two colums of a given table using seaborn or matplotib
How to plot two bar diagram for two colums of a given table using seaborn or matplotib

Time:12-26

This might be a simple task but I am new to plotting in python and is struggling to convert logic into code. I have 3 columns like below that consists of Countries, Quantities and Revenues:

Country Quantities Revenues
United Kingdom 2915836 8125479.97
EIRE 87390 253026.10
Netherlands 127083 245279.99
Germany 72068 202050.01
France 68439 184024.28
Australia 52611 122974.01
Spain 18947 56444.29
Switzerland 18769 50671.57
Belgium 12068 34926.92
Norway 10965 32184.10
Japan 14207 31914.79
Portugal 10430 30247.57
Sweden 10720 24456.55

All I want to do is creating a side by side bars for each country which would represent the revenue and quantity for each region.

So far, i have came across performing this:

sns.catplot(kind = 'bar', data = dj, y = 'Quantities,Revenues', x = 'Country', hue = 'Details')
plt.show()

But this cannot interpret the input "Country".

I hope I am making sense.

CodePudding user response:

With enter image description here

CodePudding user response:

pandas already has a built-in plotting function: .plot and you can choose which type by specifying it like; .bar(), .scatter() or using kind= and then the type; kind='bar' or kind='scatter'. So, in this situation you will use a bar.

import matplotlib.pyplot as plt # import this to show the plot

df.plot.bar(x="Country", **kwargs) # plot the bars

plt.show() # show it
  • Related