Customize Your Line Chart
Tarefa
Swipe to start coding
-
Customize the lines so that:
- the first line (for the
usa
data) will be red ('r'
), dashdotted (-.
) and with star points ('*'
); - the second line (for the
can
data) will be green ('g'
), dotted (':'
) and with circle points ('.'
).
- the first line (for the
-
Add a legend to the plot.
Solução
# 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)
# Create Figure and Axes objects
fig, ax = plt.subplots()
# Save data for the US and Canada
usa = data.loc['United States']
can = data.loc['Canada']
# Initialize the plot
ax.plot(usa.index.astype(int), usa.values, label = 'United States',
color = 'r', linestyle = '-.', marker = '*')
ax.plot(can.index.astype(int), can.values, label = 'Canada',
color = 'g', linestyle = ':', marker = '.')
# Set custom labels on axis
ax.set_xlabel('Year')
ax.set_ylabel('CO2 emission per person (metric tonnes)')
# Add plot title
plt.title('CO2 emissions (tonnes per person) in USA and Canada')
# Display the legend and the plot
plt.legend()
plt.show()
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 1. Capítulo 11
# 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)
# Create Figure and Axes objects
fig, ax = plt.subplots()
# Save data for the US and Canada
usa = data.loc['United States']
can = data.loc['Canada']
# Initialize the plot
ax.plot(usa.index.astype(int), usa.values, label = 'United States',
color = '___', linestyle = '___', ___ = '___')
ax.plot(can.index.astype(int), can.values, label = 'Canada',
___ = '___', ___ = '___', marker = '___')
# Set custom labels on axis
ax.set_xlabel('Year')
ax.set_ylabel('CO2 emission per person (metric tonnes)')
# Add plot title
plt.title('CO2 emissions (tonnes per person) in USA and Canada')
# Display the legend and the plot
___
plt.show()