Line Plots#
We can create a simple line plot using px.line()
. This example uses example data on life expectancy.
import plotly.express as px # import statements
df = px.data.gapminder().query("country=='Canada'") # load sample data, filter for Canada
df # inspect data
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada') # create plot
fig # show output
In this example, we pass a subset of the gapminder
dataframe to the px.line()
function. We specify which fields to use for the X
and Y
axis values, and we give the figure a title.
Plotting Two Variables#
Let’s say we wanted to create a line plot with data for two countries. We could filter the data frame accordingly and use the color
parameter.
df = px.data.gapminder().query("continent=='Oceania'") # new dataset, filter for region
df # inspect data
country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
---|---|---|---|---|---|---|---|---|
60 | Australia | Oceania | 1952 | 69.120 | 8691212 | 10039.59564 | AUS | 36 |
61 | Australia | Oceania | 1957 | 70.330 | 9712569 | 10949.64959 | AUS | 36 |
62 | Australia | Oceania | 1962 | 70.930 | 10794968 | 12217.22686 | AUS | 36 |
63 | Australia | Oceania | 1967 | 71.100 | 11872264 | 14526.12465 | AUS | 36 |
64 | Australia | Oceania | 1972 | 71.930 | 13177000 | 16788.62948 | AUS | 36 |
65 | Australia | Oceania | 1977 | 73.490 | 14074100 | 18334.19751 | AUS | 36 |
66 | Australia | Oceania | 1982 | 74.740 | 15184200 | 19477.00928 | AUS | 36 |
67 | Australia | Oceania | 1987 | 76.320 | 16257249 | 21888.88903 | AUS | 36 |
68 | Australia | Oceania | 1992 | 77.560 | 17481977 | 23424.76683 | AUS | 36 |
69 | Australia | Oceania | 1997 | 78.830 | 18565243 | 26997.93657 | AUS | 36 |
70 | Australia | Oceania | 2002 | 80.370 | 19546792 | 30687.75473 | AUS | 36 |
71 | Australia | Oceania | 2007 | 81.235 | 20434176 | 34435.36744 | AUS | 36 |
1092 | New Zealand | Oceania | 1952 | 69.390 | 1994794 | 10556.57566 | NZL | 554 |
1093 | New Zealand | Oceania | 1957 | 70.260 | 2229407 | 12247.39532 | NZL | 554 |
1094 | New Zealand | Oceania | 1962 | 71.240 | 2488550 | 13175.67800 | NZL | 554 |
1095 | New Zealand | Oceania | 1967 | 71.520 | 2728150 | 14463.91893 | NZL | 554 |
1096 | New Zealand | Oceania | 1972 | 71.890 | 2929100 | 16046.03728 | NZL | 554 |
1097 | New Zealand | Oceania | 1977 | 72.220 | 3164900 | 16233.71770 | NZL | 554 |
1098 | New Zealand | Oceania | 1982 | 73.840 | 3210650 | 17632.41040 | NZL | 554 |
1099 | New Zealand | Oceania | 1987 | 74.320 | 3317166 | 19007.19129 | NZL | 554 |
1100 | New Zealand | Oceania | 1992 | 76.330 | 3437674 | 18363.32494 | NZL | 554 |
1101 | New Zealand | Oceania | 1997 | 77.550 | 3676187 | 21050.41377 | NZL | 554 |
1102 | New Zealand | Oceania | 2002 | 79.110 | 3908037 | 23189.80135 | NZL | 554 |
1103 | New Zealand | Oceania | 2007 | 80.204 | 4115771 | 25185.00911 | NZL | 554 |
fig = px.line(df, x="year", y="lifeExp", color='country', title='Life Expectancy by Country') # create plot
fig # show output
Grouping#
Let’s say we want to modify the line plot to include a line for individual countries and color the lines by continent.
df = px.data.gapminder() # subset data
fig = px.line(df, x="year", y="lifeExp", color='continent', line_group='country', hover_name='country', title='Country Life Expectancy by Continent') # create figure
fig # show output
In the modified example, we color the lines by continent and group the lines by country. We also use line_group
to group rows in a column into lines and hover_name
to set a title or name for the hover labels. The country name is now at the top of each hover label.
Additional Resources#
For more on line plots in plotly
: