Customize Your Line Chart
Uppgift
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.
Lösning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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()
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 1. Kapitel 11
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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()
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal