-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_plotly_07.py
38 lines (33 loc) · 1.05 KB
/
04_plotly_07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#######
# Objective: Create a stacked bar chart from
# the file ../data/mocksurvey.csv. Note that questions appear in
# the index (and should be used for the x-axis), while responses
# appear as column labels. Extra Credit: make a horizontal bar chart!
######
# Perform imports here:
import plotly.offline as pyo
import plotly.graph_objs as go
import pandas as pd
# create a DataFrame from the .csv file:
df = pd.read_csv('data/mocksurvey.csv', index_col=0)
# create traces using a list comprehension:
# data = [
# go.Bar(
# x=df.index,
# y=df[response],
# name=response
# ) for response in df.columns
# ]
data = [
go.Bar(
x=df[response],
y=df.index,
orientation='h',
name=response
) for response in df.columns
]
# create a layout, remember to set the barmode here
layout = go.Layout(title='Surveys', barmode='stack')
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig, filename='04_plotly_07.html')
# create a fig from data & layout, and plot the fig.