Advertisement
brycewcole

Survey Analysis

Feb 19th, 2018
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. import plotly.plotly as py
  2. from plotly.graph_objs import *
  3. import pandas as pd
  4.  
  5. def main():
  6.     #Read data from spreadsheet in
  7.     fileName = 'Technical Report Survey (Responses).xlsx'
  8.     excel = pd.ExcelFile(fileName)
  9.     sheet = excel.parse('Form Responses 1')
  10.  
  11.     #Generate The First Pie Chart
  12.     MultiPie(sheet, sheet.Know, 'Percent of College Students Who Can Confidently Write A Technical Report.')
  13.     #Generate the next one
  14.     MultiPie(sheet, sheet.Heard, 'Percent of College Students Who Know What A Technical Report Is.')
  15.  
  16. def MultiPie(df, checkC, chart_name):
  17.     labels = ['Yes','No']
  18.     colors = ['#13d100','#da2700']
  19.     sValues = [
  20.         df[(checkC == 'Yes') & (df.Major == 'STEM')].count()[0],
  21.         df[(checkC == 'No') & (df.Major == 'STEM')].count()[0]
  22.     ]
  23.     bValues = [
  24.         df[(checkC == 'Yes') & (df.Major == 'Business')].count()[0],
  25.         df[(checkC == 'No') & (df.Major == 'Business')].count()[0]
  26.     ]
  27.     pValues = [
  28.         df[(checkC == 'Yes') & (df.Major == 'Pilot')].count()[0],
  29.         df[(checkC == 'No') & (df.Major == 'Pilot')].count()[0]
  30.     ]
  31.     oValues = [
  32.         df[(checkC == 'Yes') & (df.Major == 'Other')].count()[0],
  33.         df[(checkC == 'No') & (df.Major == 'Other')].count()[0]
  34.     ]
  35.  
  36.  
  37.     fig = {
  38.         'data': [
  39.             #STEM
  40.             {
  41.                 'labels': labels,
  42.                 'values': sValues,
  43.                 'marker': {'colors': colors},
  44.                 'type': 'pie',
  45.                 'name': 'STEM',
  46.                 'domain': {'x': [0, .5],
  47.                         'y': [.55, .9]},
  48.                 'hoverinfo':'label+percent+value',
  49.                 'textinfo':'none',
  50.             },
  51.             #Business
  52.             {
  53.                 'labels': labels,
  54.                 'values': bValues,
  55.                 'type': 'pie',
  56.                 'name': 'Business',
  57.                 'marker': {'colors': colors},
  58.                 'domain': {'x': [.5, .9],
  59.                         'y': [.55, .9]},
  60.                 'hoverinfo':'label+percent+value',
  61.                 'textinfo':'none',
  62.             },
  63.             #Pilot
  64.             {
  65.                 'labels': labels,
  66.                 'values': pValues,
  67.                 'marker': {'colors': colors},
  68.                 'type': 'pie',
  69.                 'name': 'Pilot',
  70.                 'domain': {'x': [0, .5],
  71.                         'y': [.1, .45]},
  72.                 'hoverinfo':'label+percent+value',
  73.                 'textinfo':'none',
  74.             },
  75.             #Other
  76.             {
  77.                 'labels': labels,
  78.                 'values': oValues,
  79.                 'marker': {'colors': colors},
  80.                 'type': 'pie',
  81.                 'name':'Other',
  82.                 'domain': {'x': [.5, .9],
  83.                         'y': [.1, .45]},
  84.                 'hoverinfo':'label+percent+value',
  85.                 'textinfo':'none',
  86.             }
  87.         ],
  88.         'layout': {
  89.                 'title': chart_name,
  90.                 'showlegend': False,
  91.                 "annotations": [
  92.                             {
  93.                                 "font": {
  94.                                     "size": 20
  95.                                 },
  96.                                 "showarrow": False,
  97.                                 "text": "STEM",
  98.                                 "x": 0.22,
  99.                                 "y": 1
  100.                             },
  101.                             {
  102.                                 "font": {
  103.                                     "size": 20
  104.                                 },
  105.                                 "showarrow": False,
  106.                                 "text": "Business",
  107.                                 "x": 0.73,
  108.                                 "y": 1
  109.                             },
  110.                             {
  111.                                 "font": {
  112.                                     "size": 20
  113.                                 },
  114.                                 "showarrow": False,
  115.                                 "text": "Pilot",
  116.                                 "x": 0.20,
  117.                                 "y": 0.5
  118.                             },
  119.                             {
  120.                                 "font": {
  121.                                     "size": 20
  122.                                 },
  123.                                 "showarrow": False,
  124.                                 "text": "Other",
  125.                                 "x": 0.74,
  126.                                 "y": 0.5
  127.                             }
  128.                         ]
  129.         }
  130.     }
  131.     #and finally plot everything!
  132.     py.iplot(fig, filename=chart_name)
  133.  
  134. if __name__ == '__main__':
  135.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement