-
Notifications
You must be signed in to change notification settings - Fork 3
/
graphfromcsv.py
executable file
·145 lines (125 loc) · 4.31 KB
/
graphfromcsv.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 16:11:05 2020
@author: william
"""
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import statsmodels.api as sm
df = pd.read_csv('/Users/william/Desktop/Python Website/output4.csv')
fig = px.scatter_ternary(df, a="Relative Upvotes", b="Relative Comments", c="Relative Downvotes", hover_name="Title", opacity = 0.7, color="Topic", size="Upvotes", size_max=9, title = 'Post reception within news Subreddits by category (2020) — Normalized Upvotes, Comments, and Downvotes', color_discrete_map = {"Donald Trump": "darkred", "Joe Biden": "blue", "Racial Justice Protests":"orange","COVID-19":"black","The Economy":"green"} )
'''trace_a = go.Scatterternary({
'mode': 'markers',
'name': 'Donald Trump',
'a': df.loc[df['Topic'] == 'Donald Trump']['Relative Upvotes'],
'b': df.loc[df['Topic'] == 'Donald Trump']['Relative Comments'],
'c': df.loc[df['Topic'] == 'Donald Trump']['Relative Downvotes'],
'text': df['Title'],
'hoverinfo': ['name','a'],
'marker': {
'color': 'red',
'size': 14,
'line': { 'width': 2 }
}} )
trace_b = go.Scatterternary({
'mode':'markers',
'name': 'Joe Biden',
'marker.size': df['Upvotes'],
'marker.sizeref':0.0001,
'a': df.loc[df['Topic'] == 'Joe Biden']['Relative Upvotes'],
'b': df.loc[df['Topic'] == 'Joe Biden']['Relative Comments'],
'c': df.loc[df['Topic'] == 'Joe Biden']['Relative Downvotes'],
'text': df['Title'],
'hoverinfo': ['name','a'],
'marker': {
'color': 'blue',
'size': 14,
'line': { 'width': 2 }
}} )
trace_c = go.Scatterternary({
'mode': 'markers',
'name': 'Racial Justice Protests',
'a': df.loc[df['Topic'] == 'Racial Justice Protests']['Relative Upvotes'],
'b': df.loc[df['Topic'] == 'Racial Justice Protests']['Relative Comments'],
'c': df.loc[df['Topic'] == 'Racial Justice Protests']['Relative Downvotes'],
'text': df['Title'],
'hoverinfo': ['name','a'],
'marker': {
'color': 'yellow',
'size': 14,
'line': { 'width': 2 }
}} )
trace_d = go.Scatterternary({
'mode': 'markers',
'name': 'COVID-19',
'a': df.loc[df['Topic'] == 'COVID-19']['Relative Upvotes'],
'b': df.loc[df['Topic'] == 'COVID-19']['Relative Comments'],
'c': df.loc[df['Topic'] == 'COVID-19']['Relative Downvotes'],
'text': df['Title'],
'hoverinfo': ['name','a'],
'marker': {
'color': 'black',
'size': 14,
'line': { 'width': 2 }
}} )
trace_e = go.Scatterternary({
'mode': 'markers',
'name': 'The Economy',
'a': df.loc[df['Topic'] == 'The Economy']['Relative Upvotes'],
'b': df.loc[df['Topic'] == 'The Economy']['Relative Comments'],
'c': df.loc[df['Topic'] == 'The Economy']['Relative Downvotes'],
'text': df['Title'],
'hoverinfo': ['name','a'],
'marker': {
'color': 'green',
'size': 14,
'line': { 'width': 2 }
}} )
fig = go.Figure([trace_a,trace_b,trace_c,trace_d,trace_e])
def makeAxis(title, tickangle):
return {
'title': title,
'titlefont': { 'size': 20 },
'tickangle': tickangle,
'tickfont': { 'size': 15 },
'tickcolor': 'rgba(0,0,0,0)',
'ticklen': 5,
'showline': True,
'showgrid': True
}
fig.update_layout({
'ternary': {
'sum': 100,
'aaxis': makeAxis('Upvotes', 0),
'baxis': makeAxis('Comments', 45),
'caxis': makeAxis('Downvotes', -45)
},
'annotations': [{
'showarrow': False,
'text': 'Plot',
'x': 0.5,
'y': 1.3,
'font': { 'size': 15 }
}]
})
scatterplot = px.scatter(
data_frame = df,
x = 'Downvotes',
y= 'Comments',
size = 'Upvotes',
facet_col='Subreddit',
marginal_x = 'violin',
size_max = 10,
color = 'Topic',
color_discrete_map = {'Donald Trump':'red','Joe Biden':'blue','Racial Justice Protests':'yellow',
'The Economy':'green','COVID-19':'black'},
hover_name = 'Title',
hover_data = ['Upvotes','Comments','Downvotes'],
labels={"Topic":"Post Topic"},
title='Comment Interaction and Downvotes on Posts in r/news and r/politics',
template='ggplot2',
)
'''
fig.write_html("/Users/william/Desktop/Python Website/ternarythree.html")