bjorn-hommel commited on
Commit
464b09c
1 Parent(s): 021a052

replaced gauge by bullet plot

Browse files
Files changed (1) hide show
  1. plots.py +93 -19
plots.py CHANGED
@@ -1,4 +1,6 @@
1
  import streamlit as st
 
 
2
  import plotly.graph_objects as go
3
  import plotly.express as px
4
 
@@ -74,29 +76,101 @@ def scatter_plot(df, group_var):
74
 
75
  def show_scores(sentiment, desirability, input_text):
76
  with st.container():
77
- p1 = indicator_plot(
78
- value=sentiment,
79
- title=f'Item Sentiment',
80
- value_range=[-1, 1],
81
- domain={'x': [0, .45], 'y': [0, .5]},
82
- )
83
 
84
- p2 = indicator_plot(
85
- value=desirability,
86
- title=f'Item Desirability',
87
- value_range=[-4, 4],
88
- domain={'x': [.55, 1], 'y': [0, .5]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  )
90
 
91
  fig = go.Figure()
92
- fig.add_trace(p1)
93
- fig.add_trace(p2)
94
-
95
- fig.update_layout(
96
- title=dict(text=f'"{input_text}"', font=dict(size=36),yref='paper'),
97
- paper_bgcolor = 'white',
98
- font = {'color': 'black', 'family': 'Arial'})
99
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  st.plotly_chart(fig, theme=None, use_container_width=True)
101
 
102
  st.markdown("""
 
1
  import streamlit as st
2
+ import matplotlib
3
+ import matplotlib.pyplot as plt
4
  import plotly.graph_objects as go
5
  import plotly.express as px
6
 
 
76
 
77
  def show_scores(sentiment, desirability, input_text):
78
  with st.container():
 
 
 
 
 
 
79
 
80
+ num_steps = 10
81
+ colorscale = plt.get_cmap('viridis', num_steps)
82
+
83
+ steps_sentiment = [{
84
+ 'range': [i/num_steps*2-1, (i+1)/num_steps*2-1],
85
+ 'color': matplotlib.colors.rgb2hex(colorscale(i)[:3])
86
+ } for i in range(num_steps)]
87
+
88
+ steps_desirability = [{
89
+ 'range': [i/num_steps*8-4, (i+1)/num_steps*8-4],
90
+ 'color': matplotlib.colors.rgb2hex(colorscale(i)[:3])
91
+ } for i in range(num_steps)]
92
+
93
+ plot1 = go.Indicator(
94
+ mode = 'number+gauge',
95
+ value = sentiment,
96
+ domain = {'x': [0.25, 1], 'y': [0.45, 0.65]},
97
+ title = {'text': 'Sentiment', 'font': {'color': 'black', 'size': 22}},
98
+ number={'font': {'color': 'black', 'size': 26}, 'valueformat': '.2f'},
99
+ gauge = {
100
+ 'shape': 'bullet',
101
+ 'axis': {'range': [-1, 1]},
102
+ 'threshold': {
103
+ 'line': {'color': '#36def1', 'width': 10},
104
+ 'thickness': 0.75,
105
+ 'value': sentiment},
106
+ 'steps': steps_sentiment,
107
+ 'bar': {'color': 'rgba(0,0,0,0)'}
108
+ }
109
+ )
110
+
111
+ plot2 = go.Indicator(
112
+ mode = 'number+gauge',
113
+ value = desirability,
114
+ domain = {'x': [0.25, 1], 'y': [0.15, 0.35]},
115
+ title = {'text': 'Desirability', 'font': {'color': 'black', 'size': 22}},
116
+ number={'font': {'color': 'black', 'size': 26}, 'valueformat': '.2f'},
117
+ gauge = {
118
+ 'shape': 'bullet',
119
+ 'axis': {'range': [-4, 4]},
120
+ 'threshold': {
121
+ 'line': {'color': '#36def1', 'width': 10},
122
+ 'thickness': 0.75,
123
+ 'value': desirability},
124
+ 'steps': steps_desirability,
125
+ 'bar': {'color': 'rgba(0,0,0,0)'}
126
+ }
127
  )
128
 
129
  fig = go.Figure()
130
+ fig.add_trace(plot1)
131
+ fig.add_trace(plot2)
132
+
133
+ plot_title = f'Estimated Sentiment and Desirability for <br><i>"{input_text}</i>"'
134
+
135
+ fig.update_layout(
136
+ annotations=[
137
+ go.layout.Annotation(
138
+ text=plot_title,
139
+ align='center',
140
+ showarrow=False,
141
+ xref='paper',
142
+ yref='paper',
143
+ x=0.5,
144
+ y=.85,
145
+ xanchor='center',
146
+ yanchor='bottom',
147
+ font=dict(size=22)
148
+ ),
149
+ go.layout.Annotation(
150
+ text="Negative",
151
+ showarrow=False,
152
+ xref='paper',
153
+ yref='paper',
154
+ x=0.25,
155
+ y=0.8,
156
+ font=dict(size=18)
157
+ ),
158
+ go.layout.Annotation(
159
+ text="Positive",
160
+ showarrow=False,
161
+ xref='paper',
162
+ yref='paper',
163
+ x=.81,
164
+ y=.8,
165
+ font=dict(size=18)
166
+ ),
167
+ ],
168
+
169
+ font = {'color': 'black', 'family': 'Arial'},
170
+ height=300,
171
+ margin={'t': 50, 'b': 0, 'l': 0}
172
+ )
173
+
174
  st.plotly_chart(fig, theme=None, use_container_width=True)
175
 
176
  st.markdown("""