Зміст курсу
Visualization in Python with matplotlib
Visualization in Python with matplotlib
Customization
You may wonder why the plot from the previous chapter didn't look like the first one. That is not the bug, just without setting the limits matplotlib
automatically set the limits such that there is a little gap between the first observations and axis. Like simple line plots, scatter plots can also be customized in several ways.
We can set the size and color of points. To set the points color use parameter c
(possible values for this parameter are the same as for color
parameter in .plot()
function from the previous section), to set the size of the points use s
parameter (either single integer/float number or array with the same size as the number of points). For example, we can relate the size of the points from the previous example to the values of 'population'
column divided by 1000000 (to make these values smaller) and make all the points red.
# Import the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # 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'], s = data['population']/1000000, c = 'red') # 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()
Дякуємо за ваш відгук!