Contenido del Curso
Visualization in Python with matplotlib
Visualization in Python with matplotlib
Simple Line Chart
That's good! Let's fill our empty plot with some data.
We can pass two iterable objects (such as, lists, arrays, tuples) as parameters for the .plot()
function, and they will be used for building the plot. By default, all the points will be connected with the blue line in the respective order as they are stored in the objects.
For example, in the following chapters you will work with the CO2 emission level data. The data is stored as a Series
object with years being indices, and CO2 levels being values. Before we start, let's inspect the data we will work with.
# Import the libraries import matplotlib.pyplot as plt import pandas as pd # Load the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0) # Inspect the data print(data.head())
Let's visualize the CO2 emission level over years for Italy.
# Import the libraries import matplotlib.pyplot as plt import pandas as pd # Load the data data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/co2.csv', index_col = 0) # Filter the data ita = data.loc['Italy'] # Create Figure and Axes objects fig, ax = plt.subplots() # Initialize the plot ax.plot(ita.index.astype(int), ita.values) # Display the plot plt.show()
Here we used indices (the .index
attribute) as values for the x-axis, and emission levels (the .values
attribute) as values for the y-axis. Values for the x-axis were converted into integers (by applying the .astype()
method since Python read them as strings initially).
Disclaimer: FREE DATA FROM WORLD BANK VIA GAPMINDER.ORG, CC-BY LICENSE.
¡Gracias por tus comentarios!