-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path01.3D_Family_Newspaper_Preferences_2.py
56 lines (47 loc) · 2.57 KB
/
01.3D_Family_Newspaper_Preferences_2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import plotly.graph_objects as go
# Given data
total_families = 10000
n_A = 0.4 * total_families
n_B = 0.2 * total_families
n_C = 0.1 * total_families
n_A_and_B = 0.05 * total_families
n_B_and_C = 0.03 * total_families
n_A_and_C = 0.04 * total_families
n_A_and_B_and_C = 0.02 * total_families
# Calculate the number of families which buy each category
n_A_only = n_A - n_A_and_B - n_A_and_C + n_A_and_B_and_C
n_B_only = n_B - n_A_and_B - n_B_and_C + n_A_and_B_and_C
n_C_only = n_C - n_A_and_C - n_B_and_C + n_A_and_B_and_C
n_none = total_families - (n_A + n_B + n_C - n_A_and_B - n_A_and_C - n_B_and_C + n_A_and_B_and_C)
n_exactly_one = n_A_only + n_B_only + n_C_only
n_A_and_B_only = n_A_and_B - n_A_and_B_and_C
n_B_and_C_only = n_B_and_C - n_A_and_B_and_C
n_A_and_C_only = n_A_and_C - n_A_and_B_and_C
n_at_least_two = n_A_and_B_and_C
n_at_most_two = total_families - n_none
n_exactly_two = n_A_and_B_and_C
# Create labels for the categories
labels = ['All three newspapers', 'Newspaper A only', 'Newspaper B only', 'Newspaper C only',
'None of A, B, C', 'Exactly one newspaper', 'Newspaper A and B only',
'Newspaper B and C only', 'Newspaper C and A only', 'At least two newspapers',
'At most two newspapers', 'Exactly two newspapers']
# Create a list of percentages for each category
percentages = [n_A_and_B_and_C/total_families*100, n_A_only/total_families*100, n_B_only/total_families*100,
n_C_only/total_families*100, n_none/total_families*100, n_exactly_one/total_families*100,
n_A_and_B_only/total_families*100, n_B_and_C_only/total_families*100,
n_A_and_C_only/total_families*100, n_at_least_two/total_families*100,
n_at_most_two/total_families*100, n_exactly_two/total_families*100]
# Create a list of colors for each category
colors = ['rgb(255, 0, 0)', 'rgb(0, 0, 255)', 'rgb(0, 255, 0)', 'rgb(255, 165, 0)',
'rgb(128, 0, 128)', 'rgb(0, 255, 255)', 'rgb(255, 0, 255)', 'rgb(255, 255, 0)',
'rgb(0, 128, 0)', 'rgb(255, 192, 203)', 'rgb(135, 206, 235)', 'rgb(139, 0, 139)']
# Create a Pie trace
fig = go.Figure(data=[go.Pie(labels=labels, values=percentages, marker=dict(colors=colors, line=dict(color='white', width=2)))],
layout=go.Layout(title='Family Newspaper Preferences', showlegend=True))
# Add hover information for each slice
fig.update_traces(hoverinfo='label+percent', textinfo='label+percent+value')
# 3D Rotation
fig.update_layout(scene=dict(camera=dict(up=dict(x=0, y=0, z=1), center=dict(x=0, y=0, z=0))),
scene_dragmode='orbit')
# Show the plot
fig.show()