update plot.py (#2)
Browse files- update plot.py (a225f2041c8af865bd0e976acf0d95291e3f702f)
Co-authored-by: handepehlivan <handepeh@users.noreply.huggingface.co>
plots.py
CHANGED
@@ -179,146 +179,30 @@ def display_heat_map(stock_df):
|
|
179 |
# Graphs the result, and displays it with a header on streamlit
|
180 |
# st.subheader('Portfolio Historical Cumulative Returns Based On Inputs!')
|
181 |
# st.line_chart(cumulative_profit)
|
182 |
-
|
183 |
-
|
184 |
-
def circle_packing(stock_df, choices):
|
185 |
symbols, weights, investment = choices.values()
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
def center_of_mass(self):
|
211 |
-
return np.average(
|
212 |
-
self.bubbles[:, :2], axis=0, weights=self.bubbles[:, 3]
|
213 |
-
)
|
214 |
-
|
215 |
-
def center_distance(self, bubble, bubbles):
|
216 |
-
return np.hypot(bubble[0] - bubbles[:, 0],
|
217 |
-
bubble[1] - bubbles[:, 1])
|
218 |
-
|
219 |
-
def outline_distance(self, bubble, bubbles):
|
220 |
-
center_distance = self.center_distance(bubble, bubbles)
|
221 |
-
return center_distance - bubble[2] - \
|
222 |
-
bubbles[:, 2] - self.bubble_spacing
|
223 |
-
|
224 |
-
def check_collisions(self, bubble, bubbles):
|
225 |
-
distance = self.outline_distance(bubble, bubbles)
|
226 |
-
return len(distance[distance < 0])
|
227 |
-
|
228 |
-
def collides_with(self, bubble, bubbles):
|
229 |
-
distance = self.outline_distance(bubble, bubbles)
|
230 |
-
idx_min = np.argmin(distance)
|
231 |
-
return idx_min if type(idx_min) == np.ndarray else [idx_min]
|
232 |
-
|
233 |
-
def collapse(self, n_iterations=50):
|
234 |
-
"""
|
235 |
-
Move bubbles to the center of mass.
|
236 |
-
|
237 |
-
Parameters
|
238 |
-
----------
|
239 |
-
n_iterations : int, default: 50
|
240 |
-
Number of moves to perform.
|
241 |
-
"""
|
242 |
-
for _i in range(n_iterations):
|
243 |
-
moves = 0
|
244 |
-
for i in range(len(self.bubbles)):
|
245 |
-
rest_bub = np.delete(self.bubbles, i, 0)
|
246 |
-
# try to move directly towards the center of mass
|
247 |
-
# direction vector from bubble to the center of mass
|
248 |
-
dir_vec = self.com - self.bubbles[i, :2]
|
249 |
-
|
250 |
-
# shorten direction vector to have length of 1
|
251 |
-
dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))
|
252 |
-
|
253 |
-
# calculate new bubble position
|
254 |
-
new_point = self.bubbles[i, :2] + dir_vec * self.step_dist
|
255 |
-
new_bubble = np.append(new_point, self.bubbles[i, 2:4])
|
256 |
-
|
257 |
-
# check whether new bubble collides with other bubbles
|
258 |
-
if not self.check_collisions(new_bubble, rest_bub):
|
259 |
-
self.bubbles[i, :] = new_bubble
|
260 |
-
self.com = self.center_of_mass()
|
261 |
-
moves += 1
|
262 |
-
else:
|
263 |
-
# try to move around a bubble that you collide with
|
264 |
-
# find colliding bubble
|
265 |
-
for colliding in self.collides_with(new_bubble, rest_bub):
|
266 |
-
# calculate direction vector
|
267 |
-
dir_vec = rest_bub[colliding, :2] - self.bubbles[i, :2]
|
268 |
-
dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))
|
269 |
-
# calculate orthogonal vector
|
270 |
-
orth = np.array([dir_vec[1], -dir_vec[0]])
|
271 |
-
# test which direction to go
|
272 |
-
new_point1 = (self.bubbles[i, :2] + orth *
|
273 |
-
self.step_dist)
|
274 |
-
new_point2 = (self.bubbles[i, :2] - orth *
|
275 |
-
self.step_dist)
|
276 |
-
dist1 = self.center_distance(
|
277 |
-
self.com, np.array([new_point1]))
|
278 |
-
dist2 = self.center_distance(
|
279 |
-
self.com, np.array([new_point2]))
|
280 |
-
new_point = new_point1 if dist1 < dist2 else new_point2
|
281 |
-
new_bubble = np.append(new_point, self.bubbles[i, 2:4])
|
282 |
-
if not self.check_collisions(new_bubble, rest_bub):
|
283 |
-
self.bubbles[i, :] = new_bubble
|
284 |
-
self.com = self.center_of_mass()
|
285 |
-
|
286 |
-
if moves / len(self.bubbles) < 0.1:
|
287 |
-
self.step_dist = self.step_dist / 2
|
288 |
-
|
289 |
-
def plot(self, ax, labels, colors):
|
290 |
-
"""
|
291 |
-
Draw the bubble plot.
|
292 |
-
|
293 |
-
Parameters
|
294 |
-
----------
|
295 |
-
ax : matplotlib.axes.Axes
|
296 |
-
labels : list
|
297 |
-
Labels of the bubbles.
|
298 |
-
colors : list
|
299 |
-
Colors of the bubbles.
|
300 |
-
"""
|
301 |
-
for i in range(len(self.bubbles)):
|
302 |
-
circ = plt.Circle(
|
303 |
-
self.bubbles[i, :2], self.bubbles[i, 2], color=colors[i])
|
304 |
-
ax.add_patch(circ)
|
305 |
-
ax.text(*self.bubbles[i, :2], labels[i],
|
306 |
-
horizontalalignment='center', verticalalignment='center')
|
307 |
-
|
308 |
-
bubble_chart = BubbleChart(area=weights, bubble_spacing=0.1)
|
309 |
-
|
310 |
-
bubble_chart.collapse()
|
311 |
-
browser_market_share = {'color': ['#5A69AF', '#579E65', '#F9C784', '#FC944A']
|
312 |
-
}
|
313 |
-
|
314 |
-
fig, ax = plt.subplots(subplot_kw=dict(aspect="equal"))
|
315 |
-
|
316 |
-
bubble_chart.plot(
|
317 |
-
ax, symbols, browser_market_share['color'])
|
318 |
-
ax.axis("off")
|
319 |
-
# ax.relim()
|
320 |
-
ax.autoscale_view()
|
321 |
-
# ax.set_title('Browser market share')
|
322 |
-
fig.patch.set_facecolor('black')
|
323 |
-
|
324 |
-
st.pyplot(fig)
|
|
|
179 |
# Graphs the result, and displays it with a header on streamlit
|
180 |
# st.subheader('Portfolio Historical Cumulative Returns Based On Inputs!')
|
181 |
# st.line_chart(cumulative_profit)
|
182 |
+
def buble_interactive(stock_df,choices):
|
183 |
+
import plotly.express as px
|
|
|
184 |
symbols, weights, investment = choices.values()
|
185 |
+
beta,cash_value_weights = ER(stock_df,choices)
|
186 |
+
my_list = []
|
187 |
+
for i in beta.values():
|
188 |
+
my_list.append(i)
|
189 |
+
|
190 |
+
df_final =pd.DataFrame()
|
191 |
+
df_final['ticker'] = symbols
|
192 |
+
df_final['quantities'] = weights
|
193 |
+
df_final['cash_value'] =cash_value_weights
|
194 |
+
df_final['Beta'] = my_list
|
195 |
+
|
196 |
+
fig = px.scatter(
|
197 |
+
df_final,
|
198 |
+
x="quantities",
|
199 |
+
y="Beta",
|
200 |
+
size="cash_value",
|
201 |
+
#color="continent",
|
202 |
+
hover_name="ticker",
|
203 |
+
log_x=True,
|
204 |
+
size_max=60,
|
205 |
+
)
|
206 |
+
fig.update_layout(title="Beta ----write something")
|
207 |
+
# -- Input the Plotly chart to the Streamlit interface
|
208 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|