MSK34 commited on
Commit
2ba232b
·
verified ·
1 Parent(s): cf9db8a

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +196 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,198 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+
6
+ st.set_page_config(
7
+ page_title="Formula 1 Data Visualization",
8
+ page_icon="🏎️",
9
+ layout="wide"
10
+ )
11
+
12
+ st.title("🏎️ Formula 1 Data Visualization")
13
+
14
+ st.write(
15
+ "Bu uygulama, 1950-2024 yılları arasındaki Formula 1 verilerini kullanarak "
16
+ "pilotlar, takımlar, yarışlar ve pistler üzerine görselleştirmeler sunar."
17
+ )
18
+
19
+
20
+ @st.cache_data
21
+ def load_data():
22
+ drivers = pd.read_csv("src/drivers.csv")
23
+ constructors = pd.read_csv("src/constructors.csv")
24
+ races = pd.read_csv("src/races.csv")
25
+ results = pd.read_csv("src/results.csv")
26
+ circuits = pd.read_csv("src/circuits.csv")
27
+ return drivers, constructors, races, results, circuits
28
+
29
+
30
+ drivers, constructors, races, results, circuits = load_data()
31
+
32
+
33
+ st.sidebar.title("Grafik Seçimi")
34
+
35
+ grafik = st.sidebar.selectbox(
36
+ "Bir grafik seçin:",
37
+ [
38
+ "Yıllara Göre Yarış Sayısı",
39
+ "En Çok Yarış Kazanan Pilotlar",
40
+ "En Başarılı Takımlar",
41
+ "Pilot Milliyetleri",
42
+ "Yarış Düzenleyen Ülkeler",
43
+ "Start Pozisyonu ve Yarış Sonucu",
44
+ "En Çok Yarış Düzenlenen Pistler"
45
+ ]
46
+ )
47
+
48
+
49
+ if grafik == "Yıllara Göre Yarış Sayısı":
50
+ st.subheader("Yıllara Göre Formula 1 Yarış Sayısı")
51
+
52
+ race_count = races.groupby("year")["raceId"].count()
53
+
54
+ fig, ax = plt.subplots(figsize=(12, 5))
55
+ ax.plot(race_count.index, race_count.values)
56
+ ax.set_xlabel("Yıl")
57
+ ax.set_ylabel("Yarış Sayısı")
58
+ ax.set_title("Yıllara Göre Formula 1 Yarış Sayısı")
59
+
60
+ st.pyplot(fig)
61
+
62
+
63
+ elif grafik == "En Çok Yarış Kazanan Pilotlar":
64
+ st.subheader("En Çok Yarış Kazanan Pilotlar")
65
+
66
+ wins = results[results["positionOrder"] == 1]
67
+
68
+ driver_wins = wins.groupby("driverId").size().reset_index(name="wins")
69
+
70
+ driver_wins = driver_wins.merge(
71
+ drivers,
72
+ on="driverId"
73
+ )
74
+
75
+ driver_wins["pilot"] = (
76
+ driver_wins["forename"] + " " + driver_wins["surname"]
77
+ )
78
+
79
+ top_drivers = driver_wins.sort_values(
80
+ "wins",
81
+ ascending=False
82
+ ).head(15)
83
+
84
+ fig, ax = plt.subplots(figsize=(10, 6))
85
+ ax.barh(top_drivers["pilot"], top_drivers["wins"])
86
+ ax.set_xlabel("Galibiyet Sayısı")
87
+ ax.set_title("En Çok Yarış Kazanan 15 Pilot")
88
+ ax.invert_yaxis()
89
+
90
+ st.pyplot(fig)
91
+
92
+
93
+ elif grafik == "En Başarılı Takımlar":
94
+ st.subheader("En Başarılı Takımlar")
95
+
96
+ wins = results[results["positionOrder"] == 1]
97
+
98
+ team_wins = wins.groupby("constructorId").size().reset_index(name="wins")
99
+
100
+ team_wins = team_wins.merge(
101
+ constructors,
102
+ on="constructorId"
103
+ )
104
+
105
+ top_teams = team_wins.sort_values(
106
+ "wins",
107
+ ascending=False
108
+ ).head(10)
109
+
110
+ fig, ax = plt.subplots(figsize=(10, 6))
111
+ ax.barh(top_teams["name"], top_teams["wins"])
112
+ ax.set_xlabel("Galibiyet Sayısı")
113
+ ax.set_title("En Başarılı 10 Formula 1 Takımı")
114
+ ax.invert_yaxis()
115
+
116
+ st.pyplot(fig)
117
+
118
+
119
+ elif grafik == "Pilot Milliyetleri":
120
+ st.subheader("Pilot Milliyetleri Dağılımı")
121
+
122
+ top_nations = drivers["nationality"].value_counts().head(10)
123
+
124
+ fig, ax = plt.subplots(figsize=(8, 8))
125
+ ax.pie(
126
+ top_nations,
127
+ labels=top_nations.index,
128
+ autopct="%1.1f%%"
129
+ )
130
+ ax.set_title("Pilot Milliyetleri Dağılımı")
131
+
132
+ st.pyplot(fig)
133
+
134
+
135
+ elif grafik == "Yarış Düzenleyen Ülkeler":
136
+ st.subheader("En Çok Yarış Düzenleyen Ülkeler")
137
+
138
+ country_count = circuits["country"].value_counts().head(15)
139
+
140
+ fig, ax = plt.subplots(figsize=(10, 6))
141
+ ax.barh(country_count.index, country_count.values)
142
+ ax.set_xlabel("Pist Sayısı")
143
+ ax.set_title("En Çok Formula 1 Pisti Bulunan Ülkeler")
144
+ ax.invert_yaxis()
145
+
146
+ st.pyplot(fig)
147
+
148
+
149
+ elif grafik == "Start Pozisyonu ve Yarış Sonucu":
150
+ st.subheader("Start Pozisyonu ve Yarış Sonucu")
151
+
152
+ sample_results = results[
153
+ results["grid"] > 0
154
+ ][["grid", "positionOrder"]].sample(
155
+ 5000,
156
+ random_state=42
157
+ )
158
+
159
+ fig, ax = plt.subplots(figsize=(8, 6))
160
+ ax.scatter(
161
+ sample_results["grid"],
162
+ sample_results["positionOrder"],
163
+ alpha=0.4
164
+ )
165
+ ax.set_xlabel("Grid Pozisyonu")
166
+ ax.set_ylabel("Yarış Sonucu")
167
+ ax.set_title("Start Pozisyonu ve Yarış Sonucu")
168
+
169
+ st.pyplot(fig)
170
+
171
+ st.write(
172
+ "Grafik, ön sıralardan başlayan pilotların yarışları üst sıralarda "
173
+ "bitirme eğiliminin daha yüksek olduğunu göstermektedir."
174
+ )
175
+
176
+
177
+ elif grafik == "En Çok Yarış Düzenlenen Pistler":
178
+ st.subheader("En Çok Yarış Düzenlenen Pistler")
179
+
180
+ circuit_count = races.groupby("circuitId").size().reset_index(name="race_count")
181
+
182
+ circuit_count = circuit_count.merge(
183
+ circuits,
184
+ on="circuitId"
185
+ )
186
+
187
+ top_circuits = circuit_count.sort_values(
188
+ "race_count",
189
+ ascending=False
190
+ ).head(15)
191
+
192
+ fig, ax = plt.subplots(figsize=(10, 6))
193
+ ax.barh(top_circuits["name"], top_circuits["race_count"])
194
+ ax.set_xlabel("Yarış Sayısı")
195
+ ax.set_title("En Çok Yarış Düzenlenen Pistler")
196
+ ax.invert_yaxis()
197
 
198
+ st.pyplot(fig)