Search code examples
pythonpython-2.7plotly

Using plotly to plot a graph


I am trying to use plotly, how can I convert it the same plot to plotly.

# Draw a nested barplot to show total loan amount for state and year
plt.style.use('bmh')
g = sns.factorplot(x="State", y="Loan_Amount_000", hue="As_of_Year", data=total_amount_group_year,
                   kind="bar",size=6, palette=sns.color_palette(flatui))
g.despine(left=True)
g.set_ylabels("Total Loan Amount",fontsize=15)
g.set_xlabels("State",fontsize=15)
g.set(title="Total Loan Amount aggregated by State and Year")
g.set_yticklabels(fontsize=15)
g.set_xticklabels(fontsize=15)

enter image description here

I am using the code below, but getting nothing shown.

import cufflinks as cf
import plotly.plotly  as py
import plotly.graph_objs as go

data = [go.Bar(x=total_amount_group_year['State'],
y=[total_amount_group_year.loc[total_amount_group_year['As_of_Year']==2012]['Loan_Amount_000'],
  total_amount_group_year.loc[total_amount_group_year['As_of_Year']==2013]['Loan_Amount_000'],
  total_amount_group_year.loc[total_amount_group_year['As_of_Year']==2014]['Loan_Amount_000']])]

layout = go.Layout(title='Iris Dataset - Species',
xaxis=dict(title='Iris Dataset - Species'),
yaxis=dict(title='Count')
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig)

This is my dataframe also showing index. the data frame is a pandas dataframe, a subset of another pandas dataframe.

State   As_of_Year  Loan_Amount_000
9   VA  2012    86144.960
10  VA  2013    72210.009
6   MD  2012    54095.591
11  VA  2014    48920.527
7   MD  2013    43640.475
8   MD  2014    28235.685
0   DC  2012    8368.582
1   DC  2013    7092.372
12  WV  2012    6023.641
13  WV  2013    5838.763
3   DE  2012    5253.819
2   DC  2014    5044.787
14  WV  2014    4984.216
4   DE  2013    4598.409
5   DE  2014    2991.961

Solution

  • Hopefully I copied the values overly correctly. I believe the issue lies in the number of arguments you're attempting to pass to y=. Your x= keyword also is using a dataframe with a different length index to y=, which I'm not sure plotly can interpret. You can use the following for loop to generate the required data traces, which yields the following chart.

    import plotly.offline as py
    import plotly.graph_objs as go
    
    py.init_notebook_mode(connected=True) 
    
    Years = [2012, 2013, 2014]
    
    data = []
    
    for Year in Years:
    
        data.append(go.Bar(x = total_amount_group_year.loc[total_amount_group_year['As_of_Year']==Year]['State'],
                y=total_amount_group_year.loc[total_amount_group_year['As_of_Year']==Year]['Loan_Amount_000'],
                name=Year))
    
    fig=go.Figure(data=data)
    py.iplot(fig)
    

    Plotly 1

    Alternatively you can use cufflinks, which I'm assuming you have already installed. This does however require you reshape your dataframe to generate the grouped bar chart. The following code yields the chart below.

    import cufflinks as cf
    
    df_pivot = total_amount_group_year.pivot(index='State', columns='As_of_Year', values='Loan_Amount_000').sort_values(2012, ascending=False)
    
    cf.set_config_file(offline=True, world_readable=False, theme='ggplot')
    
    df_pivot.iplot(kind='bar')
    

    enter image description here

    enter image description here

    Hopefully this helps!