projekt2, projekt3

This commit is contained in:
filnow 2024-06-18 12:17:52 +02:00
parent 4027c48053
commit f9bcb9c254
10 changed files with 17810 additions and 736 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,19 +1,8 @@
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"id": "c3968b76-b939-4c1c-9b84-29e51f8ae69f",
"metadata": {},
"source": [
"# Statystyki miliarderów\n",
"**Zbiór danych zawiera statystyki dotyczące miliarderów na świecie, zawarte są również ich dane osobowe, branże którymi się zajmują oraz firmy.**\n",
"\n",
"[Źródło](https://www.kaggle.com/datasets/nelgiriyewithana/billionaires-statistics-dataset)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 1,
"id": "cb8f7a05-40a5-46cf-8900-88da58e8690f", "id": "cb8f7a05-40a5-46cf-8900-88da58e8690f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -28,7 +17,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 2,
"id": "5a5c0cd6-6606-4983-b945-eeed8b5afe24", "id": "5a5c0cd6-6606-4983-b945-eeed8b5afe24",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -492,26 +481,21 @@
], ],
"source": [ "source": [
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))\n", "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))\n",
"# Plot the birth month distribution\n", "\n",
"ax1.set_title('Miesiące urodzenia miliarderów', fontsize=16)\n", "ax1.set_title('Miesiące urodzenia miliarderów', fontsize=16)\n",
"sns.histplot(data=df['birthMonth'], bins=12, color='skyblue', kde=True, ax=ax1)\n", "sns.histplot(data=df['birthMonth'], bins=12, color='skyblue', kde=True, ax=ax1)\n",
"ax1.set_xlabel('Miesiąc urodzenia', fontsize=12)\n", "ax1.set_xlabel('Miesiąc urodzenia', fontsize=12)\n",
"ax1.set_ylabel('Liczba miliarderów', fontsize=12)\n", "ax1.set_ylabel('Liczba miliarderów', fontsize=12)\n",
"\n", "\n",
"# Plot the birth day distribution\n",
"ax2.set_title('Dni urodzenia miliarderów', fontsize=16)\n", "ax2.set_title('Dni urodzenia miliarderów', fontsize=16)\n",
"sns.histplot(data=df['birthDay'], bins=31, color='skyblue', kde=True, ax=ax2)\n", "sns.histplot(data=df['birthDay'], bins=31, color='skyblue', kde=True, ax=ax2)\n",
"ax2.set_xlabel('Dzień urodzenia', fontsize=12)\n", "ax2.set_xlabel('Dzień urodzenia', fontsize=12)\n",
"ax2.set_ylabel('Liczba miliarderów', fontsize=12)\n", "ax2.set_ylabel('Liczba miliarderów', fontsize=12)\n",
"\n", "\n",
"# Set the style and remove the left despine\n",
"sns.set_style(\"whitegrid\")\n", "sns.set_style(\"whitegrid\")\n",
"sns.despine(left=True)\n", "sns.despine(left=True)\n",
"plt.grid(axis='y', linestyle='--', alpha=0.7)\n", "plt.grid(axis='y', linestyle='--', alpha=0.7)\n",
"\n",
"# Adjust the spacing between subplots\n",
"plt.subplots_adjust(wspace=0.4)\n", "plt.subplots_adjust(wspace=0.4)\n",
"\n",
"plt.show()" "plt.show()"
] ]
}, },
@ -831,7 +815,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.9" "version": "3.11.8"
} }
}, },
"nbformat": 4, "nbformat": 4,

1
dashboard/README.md Normal file
View File

@ -0,0 +1 @@
This template gives you a more "complete" dashboard for exploring the tips dataset. For an overview of what's here, visit [this article](https://shiny.posit.co/py/docs/user-interfaces.html).

Binary file not shown.

231
dashboard/app.py Normal file
View File

@ -0,0 +1,231 @@
import faicons as fa
import plotly.express as px
from shared import app_dir, billionaires
from shinywidgets import render_plotly
import pandas as pd
from shiny import reactive, render
from shiny.express import input, ui
worth_rng = (min(billionaires.finalWorth), max(billionaires.finalWorth))
ui.page_opts(title="Billionaires dataset", fillable=False)
with ui.sidebar(open="desktop"):
ui.input_slider(
"net_worth",
"Net worth range in billions",
min=worth_rng[0],
max=worth_rng[1],
value=worth_rng,
pre="$",
post="B",
)
ui.input_selectize(
"categories",
"Busniess categories",
billionaires['category'].unique().tolist(),
selected="Technology",
)
ui.input_action_button("reset", "Reset filter")
# Add main content
ICONS = {
"user": fa.icon_svg("user", "regular"),
"currency-dollar": fa.icon_svg("dollar-sign"),
"ellipsis": fa.icon_svg("ellipsis"),
"age": fa.icon_svg("hourglass-half")
}
with ui.layout_columns(fill=False):
with ui.value_box(showcase=ICONS["user"]):
"Total billionaires"
@render.express
def total_billionaires():
billionaires_data().shape[0]
with ui.value_box(showcase=ICONS["currency-dollar"]):
"Average net worth"
@render.express
def average_net_worth():
d = billionaires_data()
if d.shape[0] > 0:
avg_worth = d['finalWorth'].mean()
f"${avg_worth:,.2f}B"
with ui.value_box(showcase=ICONS["age"]):
"Average age"
@render.express
def average_bill():
d = billionaires_data()
if d.shape[0] > 0:
avg_age = d['age'].mean()
f"{avg_age:.1f}"
with ui.layout_columns(col_widths=[6, 6, 12]):
with ui.card(full_screen=True):
ui.card_header("Billionaires data")
@render.data_frame
def table():
return render.DataGrid(billionaires_data())
with ui.card(full_screen=True):
ui.card_header("Billionaires by Country")
@render_plotly
def billionaires_map():
country_counts = billionaires_data()['country'].value_counts()
country_data = pd.DataFrame({
'country': country_counts.index,
'billionaires': country_counts.values
})
fig = px.choropleth(
country_data,
locations="country",
locationmode='country names',
color="billionaires",
hover_name="country",
color_continuous_scale="Plasma",
projection="orthographic",
labels={'billionaires': 'Number of Billionaires'},
)
fig.update_layout(
geo=dict(
showframe=False,
projection_type="orthographic",
showcoastlines=True,
showocean=True,
showland=True,
showcountries=True,
landcolor='rgb(243, 243, 243)',
countrycolor='rgb(204, 204, 204)'
),
showlegend=False,
)
return fig
with ui.card(full_screen=True):
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
"Top Billionaires by Net Worth"
with ui.popover(title=None):
ICONS["ellipsis"]
ui.input_radio_buttons(
"gender_split",
"Choose gender",
["M", "F"],
selected="M",
inline=True,
)
@render_plotly
def top_billionaires_plot():
gender_filter = input.gender_split()
filtered_data = billionaires_data()[billionaires_data()["gender"] == gender_filter]
top_10 = filtered_data.nlargest(10, 'finalWorth')
fig = px.bar(
top_10,
x='finalWorth',
y='personName',
color='personName',
orientation='h',
labels={'finalWorth': 'Net Worth (Billion $)', 'personName': 'Name'},
facet_row_spacing=0.1,
)
fig.update_layout(
xaxis_title='Net Worth (Billion $)',
yaxis_title='Name',
showlegend=False,
)
return fig
with ui.layout_columns(col_widths=[6, 6]):
with ui.card(full_screen=True):
ui.card_header("Number of Billionaires and Total Tax Rate by Country'")
@render_plotly
def tabl():
billionaires_per_country = billionaires_data()['country'].value_counts().reset_index()
billionaires_per_country.columns = ['country', 'number_of_billionaires']
tax_rate_df = billionaires_data()[['country', 'total_tax_rate_country']].drop_duplicates()
merged_df = pd.merge(billionaires_per_country, tax_rate_df, on='country')
fig = px.scatter(merged_df,
x='number_of_billionaires',
y='total_tax_rate_country',
hover_name='country',
labels={
'number_of_billionaires': 'Number of Billionaires',
'total_tax_rate_country': 'Total Tax Rate (%)'
},
size='number_of_billionaires',
size_max=60,
color='total_tax_rate_country',
color_continuous_scale=px.colors.sequential.Plasma)
fig.update_layout(
title_font_size=20,
xaxis_title_font_size=16,
yaxis_title_font_size=16,
plot_bgcolor='rgba(240, 240, 240, 0.8)',
paper_bgcolor='rgba(0, 0, 0, 0)',
xaxis=dict(showgrid=True, gridcolor='lightgrey', type='log'),
yaxis=dict(showgrid=True, gridcolor='lightgrey')
)
return fig
with ui.card(full_screen=True):
ui.card_header("Billionaires by City")
@render_plotly
def billionaire():
top_birth_cities = billionaires_data()['city'].value_counts().head(20)
fig = px.bar(
top_birth_cities,
x=top_birth_cities.index,
y=top_birth_cities.values,
orientation='v',
labels={'index': 'City', 'values': 'Number of Billionaires'},
color=top_birth_cities.values,
color_continuous_scale='Viridis',
)
fig.update_traces(
customdata=top_birth_cities.values,
hovertemplate='<br>Number of Billionaires: %{customdata}'
)
fig.update_layout(
xaxis_title='Number of Billionaires',
yaxis_title='City',
showlegend=False,
)
return fig
ui.include_css(app_dir / "styles.css")
@reactive.calc
def billionaires_data():
net_worth = input.net_worth()
idx1 = billionaires['finalWorth'].between(net_worth[0], net_worth[1])
idx2 = billionaires['category'].isin([input.categories()])
return billionaires[idx1 & idx2]
@reactive.effect
@reactive.event(input.reset)
def _():
ui.update_slider("net_worth", value=worth_rng)
ui.update_checkbox_group("categories", selected=[billionaires['category'].unique().tolist()])

2641
dashboard/data.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
faicons
shiny
shinywidgets
plotly
pandas
ridgeplot

7
dashboard/shared.py Normal file
View File

@ -0,0 +1,7 @@
from pathlib import Path
import pandas as pd
app_dir = Path(__file__).parent
billionaires = pd.read_csv(app_dir / "data.csv")
billionaires["finalWorth"] = billionaires["finalWorth"] / 1000

12
dashboard/styles.css Normal file
View File

@ -0,0 +1,12 @@
:root {
--bslib-sidebar-main-bg: #f8f8f8;
}
.popover {
--bs-popover-header-bg: #222;
--bs-popover-header-color: #fff;
}
.popover .btn-close {
filter: var(--bs-btn-close-white-filter);
}

14908
projekt2.ipynb Normal file

File diff suppressed because it is too large Load Diff