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

Зміст курсу

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

Customizing 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.

Завдання

  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.

Завдання

  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.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row

Customizing 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.

Завдання

  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.

Завдання

  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.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row

Customizing 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.

Завдання

  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.

Завдання

  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.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

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.

Завдання

  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.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 5
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt