Hi there
I should plot a count bar chart of Dataframe generated from a dict.
Here is my code:
import plotly.offline as py
import plotly.graph_objs as go
import numpy as np
import pandas as pd
#use the next line if you are using notebook
py.init_notebook_mode(connected=True)
d = {'facebook': 1876, 'twitter': 512, 'linkedin': 8, 'pinterest': 18, 'google+': 97, 'instagram': 145}
count_social = pd.DataFrame.from_dict(d,orient='index')
count_social.columns= ['social']
data = [go.Histogram(x=count_social['social'],
histnorm='count',
name='Social accounts'
)]
# IPython notebook
layout = go.Layout(
bargap=0.2,
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
I want six bin on x axes (one for each social network) and count of in y values, but I am unable to get it.
Any help is appreciated!
Thanks
Hi @anitto,
Since you already know the heights of each bar (the count) you can actually use a plain Bar
trace rather than a Histogram
trace. For example
import plotly.offline as py
import plotly.graph_objs as go
import numpy as np
import pandas as pd
#use the next line if you are using notebook
py.init_notebook_mode(connected=True)
d = {'facebook': 1876, 'twitter': 512, 'linkedin': 8, 'pinterest': 18, 'google+': 97, 'instagram': 145}
count_social = pd.DataFrame.from_dict(d,orient='index')
count_social.columns= ['social']
data = [go.Bar(x=count_social.index,
y=count_social['social'],
name='Social accounts')]
# IPython notebook
layout = go.Layout(
bargap=0.2,
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
The Histogram
trace would be useful if you were starting with a list of social networks and you hadn’t yet determined the number of times each one appears. For example:
data = [go.Histogram(
x=['facebook', 'facebook', 'facebook',
'twitter',
'linkedin', 'linkedin'],
name='Social accounts')]
# IPython notebook
layout = go.Layout(
bargap=0.2,
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
I hope that helps clear things up a bit!