From a011d0123fa417378f59755ed555b2cb87ea7558 Mon Sep 17 00:00:00 2001 From: Felipe Gonzalez Date: Fri, 31 May 2024 18:29:17 -0300 Subject: [PATCH] Hotfix/issue 150 (#151) * Fix #150 * Replaces mcp module for extract_hex_colors_from_cmap() * Remove fig title params --- requirements.txt | 1 - setup.py | 3 +-- .../dashboard/pages/1_Datos Generales.py | 19 ++++++++++--- .../pages/2_Indicadores de oferta.py | 10 +++---- urbantrips/viz/viz.py | 27 ++++++++++++------- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/requirements.txt b/requirements.txt index ac76e09..43f57d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,6 @@ seaborn IPython matplotlib-scalebar numba -mycolorpy streamlit streamlit_folium Pillow diff --git a/setup.py b/setup.py index 12a8c34..7866f6c 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name='urbantrips', - version='0.2.4', + version='0.2.5', author="Felipe Gonzalez & Sebastian Anapolsky", author_email="", description="A library to process public transit smart card data.", @@ -31,7 +31,6 @@ 'libpysal==4.8.0', 'mapclassify==2.6.1', 'matplotlib-scalebar==0.8.1', - 'mycolorpy==1.5.1', 'notebook==7.0.4', 'numba==0.58.0', 'numpy==1.25.2', diff --git a/urbantrips/dashboard/pages/1_Datos Generales.py b/urbantrips/dashboard/pages/1_Datos Generales.py index 5939953..f38ea51 100644 --- a/urbantrips/dashboard/pages/1_Datos Generales.py +++ b/urbantrips/dashboard/pages/1_Datos Generales.py @@ -9,11 +9,11 @@ import mapclassify import plotly.express as px import matplotlib.pyplot as plt +from matplotlib import colors as mcolors import seaborn as sns import contextily as cx -from mycolorpy import colorlist as mcp import os - +import numpy as np import yaml import sqlite3 from shapely import wkt @@ -21,6 +21,19 @@ from shapely.geometry import LineString +def extract_hex_colors_from_cmap(cmap, n=5): + # Choose a colormap + cmap = plt.get_cmap(cmap) + + # Extract colors from the colormap + colors = cmap(np.linspace(0, 1, n)) + + # Convert the colors to hex format + hex_colors = [mcolors.rgb2hex(color) for color in colors] + + return hex_colors + + def create_linestring(df, lat_o='lat_o', lon_o='lon_o', @@ -187,7 +200,7 @@ def crear_mapa_folium(df_agg, line_w = 0.5 - colors = mcp.gen_color(cmap=cmap, n=k_jenks) + colors = extract_hex_colors_from_cmap(cmap=cmap, n=k_jenks) n = 0 for i in bins_labels: diff --git a/urbantrips/dashboard/pages/2_Indicadores de oferta.py b/urbantrips/dashboard/pages/2_Indicadores de oferta.py index df4da71..43521ff 100644 --- a/urbantrips/dashboard/pages/2_Indicadores de oferta.py +++ b/urbantrips/dashboard/pages/2_Indicadores de oferta.py @@ -11,7 +11,6 @@ import matplotlib.pyplot as plt import seaborn as sns import contextily as cx -from mycolorpy import colorlist as mcp import os from PIL import UnidentifiedImageError from requests.exceptions import ConnectionError as r_ConnectionError @@ -353,17 +352,14 @@ def plot_lineas(lineas, id_linea, nombre_linea, day_type, n_sections, rango): ) try: - prov = cx.providers.Stamen.TonerLite - cx.add_basemap(ax1, crs=gdf_d0.crs.to_string(), - source=prov, attribution_size=7) - cx.add_basemap(ax2, crs=gdf_d1.crs.to_string(), - source=prov, attribution_size=7) - except (UnidentifiedImageError, ValueError): prov = cx.providers.CartoDB.Positron cx.add_basemap(ax1, crs=gdf_d0.crs.to_string(), source=prov, attribution_size=7) cx.add_basemap(ax2, crs=gdf_d1.crs.to_string(), source=prov, attribution_size=7) + except (UnidentifiedImageError, ValueError): + cx.add_basemap(ax1, crs=gdf_d0.crs.to_string(), attribution_size=7) + cx.add_basemap(ax2, crs=gdf_d1.crs.to_string(), attribution_size=7) except (r_ConnectionError): pass diff --git a/urbantrips/viz/viz.py b/urbantrips/viz/viz.py index ed0c635..f082b1c 100644 --- a/urbantrips/viz/viz.py +++ b/urbantrips/viz/viz.py @@ -20,7 +20,6 @@ from pathlib import Path from matplotlib import colors as mcolors from matplotlib.text import Text -from mycolorpy import colorlist as mcp from requests.exceptions import ConnectionError as r_ConnectionError from pandas.io.sql import DatabaseError @@ -544,14 +543,13 @@ def viz_etapas_x_tramo_recorrido(df, shrink=0.05, edgecolor='Orange'), ) - prov = cx.providers.Stamen.TonerLite + prov = cx.providers.CartoDB.Positron try: cx.add_basemap(ax1, crs=gdf_d0.crs.to_string(), source=prov) cx.add_basemap(ax2, crs=gdf_d1.crs.to_string(), source=prov) except (UnidentifiedImageError, ValueError): - prov = cx.providers.CartoDB.Positron - cx.add_basemap(ax1, crs=gdf_d0.crs.to_string(), source=prov) - cx.add_basemap(ax2, crs=gdf_d1.crs.to_string(), source=prov) + cx.add_basemap(ax1, crs=gdf_d0.crs.to_string()) + cx.add_basemap(ax2, crs=gdf_d1.crs.to_string()) except (r_ConnectionError): pass @@ -2219,7 +2217,7 @@ def crear_mapa_folium(df_agg, line_w = 0.5 - colors = mcp.gen_color(cmap=cmap, n=k_jenks) + colors = extract_hex_colors_from_cmap(cmap=cmap, n=k_jenks) n = 0 for i in bins_labels: @@ -2519,9 +2517,7 @@ def plot_basic_kpi(kpi_by_line_hr, standarize_supply_demand=False, ax.set_xlabel("Hora") ax.set_ylabel(ylabel_str) - f.suptitle(f"Indicadores de oferta y demanda estadarizados", - fontdict={'size': 18, - 'weight': 'bold'}) + f.suptitle(f"Indicadores de oferta y demanda estadarizados") ax.set_title(f"{id_linea_str} id linea: {line_id} - Dia: {day_str}", fontdict={"fontsize": 11}) @@ -2896,3 +2892,16 @@ def create_visualizations(): # plot basic kpi if exists plot_basic_kpi_wrapper() + + +def extract_hex_colors_from_cmap(cmap, n=5): + # Choose a colormap + cmap = plt.get_cmap(cmap) + + # Extract colors from the colormap + colors = cmap(np.linspace(0, 1, n)) + + # Convert the colors to hex format + hex_colors = [mcolors.rgb2hex(color) for color in colors] + + return hex_colors