Plotting a DataFrame
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 47 of 60.
A table already holds the numbers a chart needs. By the end of this chapter, one table will give you this:
It starts with one call. df.plot takes two column names: x= across, y= up:
sales = pd.DataFrame({"day": [1, 2, 3, 4, 5], "cups": [12, 18, 15, 25, 30]})
fig, ax = plt.subplots()
sales.plot(x="day", y="cups", ax=ax)
ax.set_title("Lemonade Sales")
plt.show()This draws the chart below. pandas labels the x axis day and names the line cups in a legend, by itself:
ax=ax puts the line on your axes. Without it, pandas makes a new figure, and your title lands on an empty chart.
Challenge
MediumA river gauge logged the water level every hour through a stormy night. river.csv has hour, level_cm and rain_mm.
- Create a figure and axes.
- Use
river.plotto drawlevel_cmagainsthouron those axes. - Title it
River Level, label the y axisLevel (cm), and show it.
Yours should look like this one, from a gauge upstream:
Your program prints nothing: Coddy checks your chart and describes it in the output.
Try it yourself
import pandas as pd
import matplotlib.pyplot as plt
river = pd.read_csv("river.csv")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
7pandas DataFrames
SeriesDataFramesReading CSV FilesInspecting DataSelecting ColumnsRecap: Movie Ratings10Plotting DataFrames
Plotting a DataFrameScatter from ColumnsOne Color per GroupHistogram of a ColumnRecap: Cafe Sales2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler