Course Content
Visualization in Python with matplotlib
Visualization in Python with matplotlib
Setting Plot Parameters
Sometimes we may need to focus on a certain part of the plot. One of the ways is to filter the observations we use for plot building. But what if we don't want to modify the data we work with? The answer is simple - we can modify the chart.
To set the chart limits we use .set()
function applied to Axes
object (usually named ax
). This function has the following parameters (but not only):
xlim
,ylim
- tuple with minimum and maximum value to be displayed on x or y-axis respectively;xticks
,yticks
- list/array with ticks to be displayed on x or y-axis respectively;xlabel
,ylabel
- string with the text to be displayed on x or y-axis respectively;title
- string to be displayed as the plot title.
For instance, let's set some parameters for the example plot we considered previously.
# Import the libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt # Reading the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0) # Create Figure and Axes objects fig, ax = plt.subplots() # Initialize a scatter plot ax.scatter(data['gdp per capita'], data['internet users']) # Set some parameters ax.set(ylim = (0, 50), xlim = (0, 20000), yticks = (0, 10, 20, 30, 40, 50), title = 'Example Plot', xlabel = 'GDP per capita', ylabel = '% of people with Internet access') # Display the plot plt.show()
As we can see, using .set()
can save our time and space, since there will be no need to use separate .set_xlabel()
, .title()
methods, and so on.
Thanks for your feedback!