Matplotlib Agregar líneas de cuadrícula
Agregar líneas de cuadrícula a un gráfico
Con Pyplot, puede usar la grid()
función para agregar líneas de cuadrícula al gráfico.
Ejemplo
Agregue líneas de cuadrícula a la trama:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid()
plt.show()
Resultado:
Especificar qué líneas de cuadrícula mostrar
Puede usar el axis
parámetro en la grid()
función para especificar qué líneas de cuadrícula mostrar.
Los valores legales son: 'x', 'y' y 'ambos'. El valor predeterminado es 'ambos'.
Ejemplo
Mostrar solo líneas de cuadrícula para el eje x:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(axis = 'x')
plt.show()
Resultado:
Ejemplo
Mostrar solo líneas de cuadrícula para el eje y:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(axis = 'y')
plt.show()
Resultado:
Establecer propiedades de línea para la cuadrícula
También puede establecer las propiedades de línea de la cuadrícula, así: grid(color = ' color ', linestyle = ' linestyle ', linewidth = number ).
Ejemplo
Establezca las propiedades de línea de la cuadrícula:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()