Contenido del Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
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:
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()
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:
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()
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.
Swipe to show code editor
- Use the appropriate function to customize the grid lines.
- Make the grid lines visible via correctly specifying the leftmost argument.
- Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
- Set the color of the grid lines to
'slategrey'
via specifying the third parameter. - Set the transparency of the grid lines to
0.5
via specifying the rightmost parameter.
¡Gracias por tus comentarios!
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:
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()
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:
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()
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.
Swipe to show code editor
- Use the appropriate function to customize the grid lines.
- Make the grid lines visible via correctly specifying the leftmost argument.
- Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
- Set the color of the grid lines to
'slategrey'
via specifying the third parameter. - Set the transparency of the grid lines to
0.5
via specifying the rightmost parameter.
¡Gracias por tus comentarios!
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:
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()
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:
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()
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.
Swipe to show code editor
- Use the appropriate function to customize the grid lines.
- Make the grid lines visible via correctly specifying the leftmost argument.
- Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
- Set the color of the grid lines to
'slategrey'
via specifying the third parameter. - Set the transparency of the grid lines to
0.5
via specifying the rightmost parameter.
¡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:
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()
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:
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()
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.
Swipe to show code editor
- Use the appropriate function to customize the grid lines.
- Make the grid lines visible via correctly specifying the leftmost argument.
- Apply the customization only to the grid lines parallel to the x-axis via specifying the second parameter.
- Set the color of the grid lines to
'slategrey'
via specifying the third parameter. - Set the transparency of the grid lines to
0.5
via specifying the rightmost parameter.