Last active
April 19, 2020 22:47
-
-
Save nicoguaro/55a6ff1a7b5a14ddddce6141a0ab3322 to your computer and use it in GitHub Desktop.
Gráfico de una curva normal con anotaciones.
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
# -*- coding: utf-8 -*- | |
""" | |
Grafica una curva normal con anotaciones. | |
@author: Nicolás Guarín-Zapata | |
@date: Abril 2020 | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
#%% Configuración de gráficos | |
plt.xkcd() | |
plt.rcParams["axes.spines.right"] = False | |
plt.rcParams["axes.spines.top"] = False | |
plt.rcParams["grid.linestyle"] = "--" | |
plt.rcParams["grid.linewidth"] = 1 | |
plt.rcParams["xtick.major.width"] = 0 | |
plt.rcParams["ytick.major.width"] = 0 | |
#%% Parámetors | |
mu = 0 | |
sigma = 1 | |
npts = 300 | |
x = np.linspace(mu - 3*sigma, mu + 3*sigma, npts) | |
y = np.exp(-0.5*(x - mu)**2/sigma**2)/(sigma*np.sqrt(2*np.pi)) | |
#%% Gráficos | |
plt.figure(figsize=(9, 5)) | |
plt.plot(x, y, color="#f95a49", lw=3, zorder=4) | |
xticks = [mu + k*sigma for k in range(-3, 4)] | |
xlabels = ['μ - 3σ', 'μ - 2σ', 'μ - σ', 'μ', 'μ + σ', 'μ + 2σ', 'μ + 3σ'] | |
plt.xticks(xticks, xlabels, fontname="Comic Sans MS") | |
plt.yticks([0, 0.1, 0.2, 0.3, 0.4]) | |
plt.xlabel("Valor") | |
plt.ylabel("Densidad") | |
plt.grid(axis="x", zorder=0) | |
plt.title("Curva normal", loc="left") | |
# Anotaciones | |
plt.plot([-1, 1], [0.15, 0.15], color="#02bfc5", zorder=3) | |
plt.text(0, 0.16, "68.2%", color="#02bfc5", ha="center", va="bottom") | |
plt.plot([-2, 2], [0.05, 0.05], color="#02bfc5", zorder=3) | |
plt.text(0, 0.06, "95.4%", color="#02bfc5", ha="center", va="bottom") | |
plt.plot([-3, 3], [0, 0], color="#02bfc5", zorder=3) | |
plt.text(0., 0.01, "99.7%", color="#02bfc5", ha="center", va="bottom") | |
plt.savefig("curva_normal.png", bbox_inches="tight", dpi=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment