import seaborn as sns penguins = sns.load_dataset("penguins") penguins # Axes-level sns.histplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack") # Figure-level sns.displot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack") # Multiples figuras (Figure-level) sns.displot(data=penguins, x="flipper_length_mm", hue="species", col="species") # Axis level import matplotlib.pyplot as plt f, axs = plt.subplots(1, 2, figsize=(8, 4), gridspec_kw=dict(width_ratios=[4, 3])) sns.scatterplot(data=penguins, x="flipper_length_mm", y="bill_length_mm", hue="species", ax=axs[0]) sns.histplot(data=penguins, x="species", hue="species", shrink=.8, alpha=.8, legend=False, ax=axs[1]) f.tight_layout() # Figure-level tips = sns.load_dataset("tips") g = sns.relplot(data=tips, x="total_bill", y="tip") # Lineplot flights = sns.load_dataset("flights") flights.head() # Axis-level may_flights = flights.query("month == 'May'") sns.lineplot(data=may_flights, x="year", y="passengers") # Axis-level flights_wide = flights.pivot("year", "month", "passengers") flights_wide.head() sns.lineplot(data=flights_wide) tips = sns.load_dataset("tips") tips.head() # Scatterplot sns.scatterplot(data=tips, x="total_bill", y="tip") sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time") # Barplot import seaborn as sns sns.set_theme(style="whitegrid") tips = sns.load_dataset("tips") ax = sns.barplot(x="day", y="total_bill", data=tips) sns.barplot(x="day", y="total_bill", hue="sex", data=tips) # Histograma penguins = sns.load_dataset("penguins") # Axis-level sns.histplot(data=penguins, x="flipper_length_mm") # Axis-level sns.histplot(data=penguins, x="flipper_length_mm", hue="species") import seaborn as sns sns.set_theme(style="whitegrid") tips = sns.load_dataset("tips") # Axis-level ax = sns.boxplot(x=tips["total_bill"]) # Axis-level ax = sns.boxplot(x="day", y="total_bill", data=tips) from google.colab import drive import os drive.mount('/content/gdrive') %cd '/content/gdrive/MyDrive/' import pandas as pd df_lluvias = pd.read_csv('pune_1965_to_2002.csv') df_lluvias.head() df_lluvias.index = df_lluvias['Year'] df_lluvias = df_lluvias.drop('Year', axis='columns') df_lluvias fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(12, 5), sharex=True, sharey=True) ax[0].plot(df_lluvias.index, df_lluvias['Jan'], label='Precipitaciones de enero') ax[1].plot(df_lluvias.index, df_lluvias['Feb'], label='Precipitaciones de febrero', color='C1') ax[2].plot(df_lluvias.index, df_lluvias['Mar'], label='Precipitaciones de marzo', color='C2') ax[0].set_title('Precipitaciones de los primeros tres meses del año') ax[2].set_xlabel('Año') ax[1].set_ylabel('Precipitación (mm.)') ax[0].legend() ax[1].legend() ax[2].legend() # FacetGrid tips = sns.load_dataset("tips") g = sns.FacetGrid(tips, col="time", row="sex") g.map(sns.scatterplot, "total_bill", "tip") g = sns.FacetGrid(tips, col="time", hue="sex") g.map_dataframe(sns.scatterplot, x="total_bill", y="tip") g.add_legend() import matplotlib as mpl mpl.rcParams['axes.titleweight'] = 'bold' mpl.rcParams['axes.titlelocation'] = 'left' mpl.rcParams['axes.titlecolor'] = 'firebrick' mpl.rcParams['axes.labelcolor'] = 'blue' mpl.rcParams['axes.labelsize'] = '10' mpl.rcParams['axes.labelweight'] = 'light' mpl.rcParams['axes.linewidth'] = '1' mpl.rcParams['grid.color'] = 'black' mpl.rcParams['grid.linestyle'] = '-.' mpl.rcParams['grid.linewidth'] = '2' fig, ax = plt.subplots(figsize=(7, 4)) ax.scatter(df_lluvias['Aug'], df_lluvias['Sep'], c=df_lluvias.index) ax.set_title('(Título rojo en negrita)') ax.set_xlabel('(Etiqueta eje horiz.)') ax.set_ylabel('(Etiqueta eje vert.)') # Restaurar por defecto mpl.rcParams.update(mpl.rcParamsDefault) mpl.rcParams.keys() mpl.rcParams sns.set_style(style="darkgrid", rc={"grid.color": ".6"}) sns.set_style(rc={"grid.linestyle": ":"}) sns.set_style(rc={"axes.titleweight": "normal"}) sns.set_style(rc={"axes.titlelocation": "left"}) sns.set_style(rc={"axes.titlecolor": "blue"}) sns.set_style(rc={"axes.labelcolor": "red"}) sns.set_style(rc={"axes.labelsize": "12"}) sns.set_style(rc={"axes.labelweight": "normal"}) sns.set_style(rc={"axes.linewidth": "0.5"}) sns.set_style(rc={"grid.color": "purple"}) sns.set_style(rc={"grid.linestyle": "--"}) sns.set_style(rc={"grid.linewidth": "0.5"}) sns.set_style(rc={"font.fantasy": "Comic Sans MS"}) sns.set_style(rc={"font.serif": "Utopia"}) penguins = sns.load_dataset("penguins") # Axis-level sns.histplot(data=penguins, x="flipper_length_mm") sns.reset_orig() penguins = sns.load_dataset("penguins") # Axis-level sns.histplot(data=penguins, x="flipper_length_mm") # Ejemplo aplicado df_ejercicio = sns.load_dataset('exercise') df_ejercicio = df_ejercicio.drop('Unnamed: 0', axis='columns') df_ejercicio.head() df_30_min = df_ejercicio[df_ejercicio['time'] == '30 min'] df_30_min.head() plt.figure() # Figure -level ax = sns.displot(data=df_30_min, x='pulse', kind='kde', hue='kind', fill=True) ax.set(xlabel='Frecuencia Cardíaca', ylabel='Densidad', title='Distribución de las pulsaciones') ax = sns.catplot(data=df_ejercicio, kind='violin', x='time', y='pulse', hue='diet', split=True) ax.set(xlabel='Duración de ejercicio', ylabel='Frecuencia cardíaca', title='Categorización de la distribución de pulsaciones')