Los gráficos creados en Python se pueden diseñar utilizando algunos de los métodos apropiados de las bibliotecas de gráficos. En este tutorial, veremos la implementación de anotaciones, leyendas y fondo del gráfico. Continuaremos usando el código del capÃtulo anterior y lo modificaremos para agregar estos estilos al diagrama.
A menudo necesitamos anotar un diagrama resaltando lugares especÃficos en el diagrama. En el siguiente ejemplo, indicamos un cambio abrupto en los valores en el gráfico agregando anotaciones en estos puntos.
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)
#Annotate
plt.annotate(xy=[2,1], s="Second Entry")
plt.annotate(xy=[4,6], s="Third Entry")
Su Salida como sigue –

A veces necesitamos un gráfico con varias lÃneas. El uso de una leyenda representa el valor asociado con cada lÃnea. En el siguiente diagrama, tenemos 3 lÃneas con los subtÃtulos correspondientes.
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)
#Annotate
plt.annotate(xy=[2,1], s="Second Entry")
plt.annotate(xy=[4,6], s="Third Entry")
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4)
Su Salida como sigue –

Podemos cambiar el estilo de presentación del gráfico utilizando varios métodos del paquete de estilo.
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0,10)
y = x ^ 2
z = x ^ 3
t = x ^ 4
# Labeling the Axes and Title
plt.title("Graph Drawing")
plt.xlabel("Time")
plt.ylabel("Distance")
plt.plot(x,y)
#Annotate
plt.annotate(xy=[2,1], s="Second Entry")
plt.annotate(xy=[4,6], s="Third Entry")
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4)
#Style the background
plt.style.use('fast')
plt.plot(x,z)
Su Salida como sigue –

🚫