Grouped Bar chart for Grouped Dataframe for my x Object - python

I am new in this field and I want to create grouped bar graph for the given grouped df.
Can anyone help me?
x = df.groupby(["year","continent"])[["lifeExp"]].mean()
The above is the grouped df that I have created but I don't know how to create grouped bar graph where x-axis should be year y-axis is life_exp and for different continent on xaxis.
My x object for grouped df
Need my grouped graph chart like this
I am not able to come up to any solution yet as i recently started my study in data visualization

Related

HI, I want to visualise a specific row plot for all columns in a dataframe using plotly

In the dataframe attached, I want to plot all the columns(fat, saturates, sugars, salt and cal) for diet_coke against date as the x axis for all the dates using plotly barchart function.
dataframe

Plot a graph in matplotlib with two different scales on one axis

I'm trying to plot a graph with time data on X-Axis. My data has daily information, but I want to create something that has two different date scales on X-Axis.
I want to start it from 2005 and it goes to 2014, but after 2014, I want that, the data continues by months of 2015. Is this possible to do? If so: how can I create this kind of plot?
Thanks.
I provided an image below:
Yes you can, just use the following pattern as I observed your X-axis values are already the same so it would just plot the other graph on the right
For a dataframe:
import numpy, matplotlib
data = numpy.array([45,63,83,91,101])
df1 = pd.DataFrame(data, index=pd.date_range('2005-10-09', periods=5, freq='W'), columns=['events'])
df2 = pd.DataFrame(numpy.arange(10,21,2), index=pd.date_range('2015-01-09', periods=6, freq='M'), columns=['events'])
matplotlib.pyplot.plot(df1.index, df1.events)
matplotlib.pyplot.plot(df2.index, df2.events)
matplotlib.pyplot.show()
You can change the parameters according to your convenience.

Adjusting the Scale on Bar Chart in Pandas

So I am plotting to visualize the education difference between genders in a given dataset.
I group the employees by gender, summing their years_in_education with this code
df1 = df[["gender","years_in_education"]] #creating a sub-dataframe with only the columns of gender and hourly wage
staff4=df1.groupby(['gender']).sum() #grouping the data frame by gender and assigning it to a new variable 'staff4'
staff4.head() #creating a visual of the grouped data for inspection
Then I use a bar chart to plot the difference with this code >>
my_plot = staff4.T.plot(kind='bar',title="Education difference between Genders") #creating the parameters to plot the graph
The graph comes out as this >>
But I observe the that scale of the y-axis is outrageous as the highest year in employment by the data is 30. I intend to adjust the scale to range from 0 - 30. I did that using my_plot.set_ylim([0,30]) and the result of that was >>
This graph is not reflective of the data as shown in the first. What can I do to change that?
Any ideas pls? How can I also change the orientation of the label on the y-axis.

how to draw a stacked bar

I have a dataframe - df as below :
df = pd.DataFrame({"Card_name":['AAA','AAA','AAA','BBB','BBB','BBB','CCC','CCC','CCC'],
"Amount":['900','800','700','600','500','400','400','300','200'],
"Category" :['Grocery','Bank','Gas','Bank','Grocery','Recreation',
'Bank','Grocery','Gas']})
I want to build a visualization plot, where i can show for all the "Card_name" the Categories along with the amount. Maybe a stacked bar chart which shows all the categories for each "Card_name". Each area(size of the area) in the stacked bar chart depends on the Amount.
I tried many possible ways but i am not able to visualize ? Any help will be appreciated.
First pivot your df, then call give the option stacked=True:
df = pd.DataFrame({"Card_name":['AAA','AAA','AAA','BBB','BBB','BBB','CCC','CCC','CCC'],
"Amount":['900','800','700','600','500','400','400','300','200'],
"Category" :['Grocery','Bank','Gas','Bank','Grocery','Recreation','Bank','Grocery','Gas']})
df['Amount'] = pd.to_numeric(df['Amount'])
df.pivot(index='Card_name', columns='Category', values='Amount').plot(kind='bar', stacked=True)

Holoviews - How to create side-by-side bars from dataframe columns?

I would like to create a Holoviews bar chart (using Bokeh backend) in which Year is on the X-axis and columns A and B on the Y-Axis. For each Year, I want bars for values from columns A and B to appear next to each other i.e. for Year 2008, I have bars of heights 1 and 3, for year 2009, I have bars 3 and 6 height, and so on. I have tried numerous different ways including the example of grouped bars in the documentation but can't get it to work. See example below:
%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
print(df)
bars = hv.Bars(df, kdims=['Year'], vdims=['A'])
bars
Please help. I am losing my mind!
HoloViews generally works best when your data is in what's called a tidy format. However to make it easier to work with data like yours we have developed a companion library called hvPlot. To generate the plot you want you can simply run:
import hvplot.pandas
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
df.hvplot.bar('Year')
Alternatively you can learn about the pd.melt method, which can take your data in a wide format and convert it to a tidy dataset:
%%opts Bars [xrotation=90 width=600 show_legend=False tools=['hover']]
df=pd.DataFrame({'Year':[2008,2009,2010,2011,2012,2013],
'A': [1,2,3,4,5,6],'B':[3,6,9,12,15,18]})
tidy_df = df.melt(id_vars=['Year'], value_vars=['A', 'B'])
bars = hv.Bars(tidy_df, ['Year', 'variable'], ['value'])
bars
To respond to #pongo30 you can suppress printing 'A' and 'B' on the x-axis by adding .opts(xlabel='') to the call to hvplot.bar() (ex: df.hvplot.bar('Year').opts(xlabel=''))

Categories

Resources