Customizing the Plot

3.2. Customizing the Plot#

matplotlib provides numerous options for customizing your plots. Let’s make some modifications to our previous plot.

plt.figure(figsize=(4,2))
plt.plot(x, y, linestyle='--', linewidth=5, color='r')
plt.xlim(2, 8) 
plt.ylim(15, 25)
plt.xlabel('X-axis', fontsize=14, color='b')
plt.ylabel('Y-axis', fontsize=14, color='b')
plt.title('Customized plot', fontsize=18)
plt.grid(True)
plt.show()  
../_images/93e8264a33acab29cd58ed7105ad9b6aae050b4ef23f78a43c21b9195434a000.png

Let’s break it down

  1. plt.figure(figsize=(4,2)): Creates a new plot with a width of 4 units and height of 2 units.

  2. plt.plot(x, y, linestyle='--', linewidth=5, color='r'): Plots the x and y values with a dashed line style, a line width of 5 units, and a red color.

  3. plt.xlim(2, 8): Sets the x-axis limits to range from 2 to 8.

  4. plt.ylim(10, 30): Sets the y-axis limits to range from 15 to 25.

  5. plt.xlabel('X-axis', fontsize=14, color='b'): Adds a blue x-axis label with a font size of 14 units.

  6. plt.ylabel('Y-axis', fontsize=14, color='b'): Adds a blue y-axis label with a font size of 14 units.

  7. plt.title('Customized plot', fontsize=18): Sets the plot’s title to ‘Customized plot’ with a font size of 18 units.

  8. plt.grid(True): Adds a grid to the plot.

  9. plt.show(): Displays the plot on the screen.