Matplotlib Rasterlijnen toevoegen
Rasterlijnen toevoegen aan een plot
Met Pyplot kunt u de grid()
functie gebruiken om rasterlijnen aan de plot toe te voegen.
Voorbeeld
Voeg rasterlijnen toe aan de plot:
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()
Resultaat:
Specificeer welke rasterlijnen moeten worden weergegeven
U kunt de axis
parameter in de grid()
functie gebruiken om aan te geven welke rasterlijnen moeten worden weergegeven.
Juridische waarden zijn: 'x', 'y' en 'beide'. De standaardwaarde is 'beide'.
Voorbeeld
Geef alleen rasterlijnen voor de x-as weer:
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()
Resultaat:
Voorbeeld
Geef alleen rasterlijnen voor de y-as weer:
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()
Resultaat:
Lijneigenschappen instellen voor het raster
U kunt de lijneigenschappen van het raster ook als volgt instellen: raster(kleur = ' kleur ', lijnstijl = ' lijnstijl ', lijnbreedte = getal ).
Voorbeeld
Stel de lijneigenschappen van het raster in:
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()