Created
September 28, 2024 20:20
-
-
Save SnowRunescape/143bc1217656077c4ab5d2879331dd57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| # Lendo o arquivo CSV e tratando possíveis erros | |
| try: | |
| data = pd.read_csv('data.csv') | |
| except FileNotFoundError: | |
| print("Arquivo não encontrado. Verifique o nome do arquivo e tente novamente.") | |
| exit() | |
| # Convertendo a coluna 'date_integer' para o tipo datetime | |
| data['date'] = pd.to_datetime(data['date_integer'], format='%Y%m%d') | |
| # Agrupando os dados pelo dia da semana e calculando a média de jogadores | |
| avg_players_by_day = data.groupby('day_of_week')['avg_players2'].mean().reset_index() | |
| # Agrupando os dados por mês e calculando a média de jogadores | |
| data['month'] = data['date'].dt.to_period('M') # Extraindo o mês | |
| avg_players_by_month = data.groupby('month')['avg_players2'].mean().reset_index() | |
| # Ordenando os dias da semana | |
| days_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] | |
| avg_players_by_day['day_of_week'] = pd.Categorical(avg_players_by_day['day_of_week'], categories=days_order, ordered=True) | |
| avg_players_by_day = avg_players_by_day.sort_values('day_of_week') | |
| # Definindo cores baseadas na média | |
| colors_day = ['lightcoral' if x < 1500 else 'lightgreen' for x in avg_players_by_day['avg_players2']] | |
| colors_month = ['skyblue'] * len(avg_players_by_month) # Cor fixa para meses | |
| # Criando o gráfico | |
| plt.figure(figsize=(14, 7)) | |
| # Gráfico de barras para média por dia da semana | |
| plt.subplot(2, 1, 1) | |
| bars_day = plt.bar(avg_players_by_day['day_of_week'], avg_players_by_day['avg_players2'], color=colors_day) | |
| plt.title('Média de Jogadores por Dia da Semana', fontsize=16) | |
| plt.xlabel('Dia da Semana', fontsize=14) | |
| plt.ylabel('Média de Jogadores', fontsize=14) | |
| plt.xticks(fontsize=12) | |
| plt.yticks(fontsize=12) | |
| plt.grid(axis='y') | |
| # Adicionando valores no topo de cada barra do gráfico diário | |
| for bar in bars_day: | |
| yval = bar.get_height() | |
| plt.text(bar.get_x() + bar.get_width()/2, yval, round(yval, 1), ha='center', va='bottom', fontsize=10) | |
| # Gráfico de barras para média por mês | |
| plt.subplot(2, 1, 2) | |
| bars_month = plt.bar(avg_players_by_month['month'].astype(str), avg_players_by_month['avg_players2'], color=colors_month) | |
| plt.title('Média de Jogadores por Mês', fontsize=16) | |
| plt.xlabel('Mês', fontsize=14) | |
| plt.ylabel('Média de Jogadores', fontsize=14) | |
| plt.xticks(rotation=45, fontsize=12) | |
| plt.yticks(fontsize=12) | |
| plt.grid(axis='y') | |
| # Adicionando valores no topo de cada barra do gráfico mensal | |
| for bar in bars_month: | |
| yval = bar.get_height() | |
| plt.text(bar.get_x() + bar.get_width()/2, yval, round(yval, 1), ha='center', va='bottom', fontsize=10) | |
| plt.tight_layout() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment