Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Customizing Grid | Plots Customization
Ultimate Visualization with Python
course content

Contenido del Curso

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookCustomizing Grid

Another important part of the customization is grid customization. pyplot module has a grid() function for this purpose.

Visibility and Axes

Its first parameter visible specifies whether to show the grid lines (by default, they are not shown).

axis parameter specifies whether to apply the customization to the grid lines parallel to the x-axis (set to 'y'), to the y-axis (set to 'x') or both of them (set to 'both'), which is the default value. Let’s clarify all of this with an example:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Setting the horizontal grid lines to be visible plt.grid(True, axis='x') plt.legend() plt.show()
copy

In this example we set visible to True, however, since we also set axis='x', it only applies to the grid lines parallel to the y-axis. Hence why there are only vertical grid lines shown, which makes the plot more detailed, yet it is not cluttered with perpendicular grid lines, which are not necessary here.

Color and Transparency

It is also possible to change the color of the grid lines using the color parameter and their transparency using the alpha parameter. Both of these parameters are already familiar to us, so let’s see them in action:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Customizing the horizontal grid lines plt.grid(True, axis='x', alpha=0.2, color='black') plt.legend() plt.show()
copy

Now our grid lines are black (color='black') and are more transparent (alpha=0.2) which makes the plot look even better.

There are still more possible parameters for the grid() functions (they are not used so often), so here is its documentation in case you want to explore more.

Tarea

  1. Use the appropriate function to customize the grid lines.
  2. Make the grid lines visible via correctly specifying the leftmost argument.
  3. Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
  4. Set the color of the grid lines to 'slategrey' via specifying the third parameter.
  5. Set the transparency of the grid lines to 0.5 via specifying the rightmost parameter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
toggle bottom row

bookCustomizing Grid

Another important part of the customization is grid customization. pyplot module has a grid() function for this purpose.

Visibility and Axes

Its first parameter visible specifies whether to show the grid lines (by default, they are not shown).

axis parameter specifies whether to apply the customization to the grid lines parallel to the x-axis (set to 'y'), to the y-axis (set to 'x') or both of them (set to 'both'), which is the default value. Let’s clarify all of this with an example:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Setting the horizontal grid lines to be visible plt.grid(True, axis='x') plt.legend() plt.show()
copy

In this example we set visible to True, however, since we also set axis='x', it only applies to the grid lines parallel to the y-axis. Hence why there are only vertical grid lines shown, which makes the plot more detailed, yet it is not cluttered with perpendicular grid lines, which are not necessary here.

Color and Transparency

It is also possible to change the color of the grid lines using the color parameter and their transparency using the alpha parameter. Both of these parameters are already familiar to us, so let’s see them in action:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Customizing the horizontal grid lines plt.grid(True, axis='x', alpha=0.2, color='black') plt.legend() plt.show()
copy

Now our grid lines are black (color='black') and are more transparent (alpha=0.2) which makes the plot look even better.

There are still more possible parameters for the grid() functions (they are not used so often), so here is its documentation in case you want to explore more.

Tarea

  1. Use the appropriate function to customize the grid lines.
  2. Make the grid lines visible via correctly specifying the leftmost argument.
  3. Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
  4. Set the color of the grid lines to 'slategrey' via specifying the third parameter.
  5. Set the transparency of the grid lines to 0.5 via specifying the rightmost parameter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
toggle bottom row

bookCustomizing Grid

Another important part of the customization is grid customization. pyplot module has a grid() function for this purpose.

Visibility and Axes

Its first parameter visible specifies whether to show the grid lines (by default, they are not shown).

axis parameter specifies whether to apply the customization to the grid lines parallel to the x-axis (set to 'y'), to the y-axis (set to 'x') or both of them (set to 'both'), which is the default value. Let’s clarify all of this with an example:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Setting the horizontal grid lines to be visible plt.grid(True, axis='x') plt.legend() plt.show()
copy

In this example we set visible to True, however, since we also set axis='x', it only applies to the grid lines parallel to the y-axis. Hence why there are only vertical grid lines shown, which makes the plot more detailed, yet it is not cluttered with perpendicular grid lines, which are not necessary here.

Color and Transparency

It is also possible to change the color of the grid lines using the color parameter and their transparency using the alpha parameter. Both of these parameters are already familiar to us, so let’s see them in action:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Customizing the horizontal grid lines plt.grid(True, axis='x', alpha=0.2, color='black') plt.legend() plt.show()
copy

Now our grid lines are black (color='black') and are more transparent (alpha=0.2) which makes the plot look even better.

There are still more possible parameters for the grid() functions (they are not used so often), so here is its documentation in case you want to explore more.

Tarea

  1. Use the appropriate function to customize the grid lines.
  2. Make the grid lines visible via correctly specifying the leftmost argument.
  3. Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
  4. Set the color of the grid lines to 'slategrey' via specifying the third parameter.
  5. Set the transparency of the grid lines to 0.5 via specifying the rightmost parameter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Another important part of the customization is grid customization. pyplot module has a grid() function for this purpose.

Visibility and Axes

Its first parameter visible specifies whether to show the grid lines (by default, they are not shown).

axis parameter specifies whether to apply the customization to the grid lines parallel to the x-axis (set to 'y'), to the y-axis (set to 'x') or both of them (set to 'both'), which is the default value. Let’s clarify all of this with an example:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Setting the horizontal grid lines to be visible plt.grid(True, axis='x') plt.legend() plt.show()
copy

In this example we set visible to True, however, since we also set axis='x', it only applies to the grid lines parallel to the y-axis. Hence why there are only vertical grid lines shown, which makes the plot more detailed, yet it is not cluttered with perpendicular grid lines, which are not necessary here.

Color and Transparency

It is also possible to change the color of the grid lines using the color parameter and their transparency using the alpha parameter. Both of these parameters are already familiar to us, so let’s see them in action:

12345678910111213
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) # Customizing the horizontal grid lines plt.grid(True, axis='x', alpha=0.2, color='black') plt.legend() plt.show()
copy

Now our grid lines are black (color='black') and are more transparent (alpha=0.2) which makes the plot look even better.

There are still more possible parameters for the grid() functions (they are not used so often), so here is its documentation in case you want to explore more.

Tarea

  1. Use the appropriate function to customize the grid lines.
  2. Make the grid lines visible via correctly specifying the leftmost argument.
  3. Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
  4. Set the color of the grid lines to 'slategrey' via specifying the third parameter.
  5. Set the transparency of the grid lines to 0.5 via specifying the rightmost parameter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt