Зміст курсу
Visualization in Python with matplotlib
Visualization in Python with matplotlib
Making Chart Informative
Let's improve the last chart with some information!
To improve the 'readability' of this plot, we can set the labels on the axis, set the title for the chart, and add a label to the colormap legend. All the things but the last one can be done within the .set
function applied to the Axes
object. Within the .set()
function we can pass xlabel
, ylabel
and title
to set labels for the x-axis, y-axis, and title for an entire plot respectively. To add the title for colormap legend, we first need to assign .colorbar()
to some variable. Then, we need to call Axes
object (usually ax
) and call .set_xlabel()
(or .set_ylabel()
for vertical text) function passing the required label as an argument.
# 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 and colorbar cax = ax.scatter(data['gdp per capita'], data['internet users'], c = data['life exp'], cmap = 'cividis') # Add information on chart ax.set(xlabel = 'GDP per capita, $', ylabel = 'Share of population with Internet access, %', title = 'GDP per capita, Internet availability and Life expectancy') # Add colormap and its legend cbar = fig.colorbar(cax) cbar.ax.set_xlabel('Life expectancy') # Display the plot plt.show()
Дякуємо за ваш відгук!