🎓 A1Learner
🔍
0 of 50 lessons completed

Lines and Dots

A line chart joins points in order, and a scatter chart shows dots with no joins. DataFrame.plot draws both from a frame.

A line from a frame

Units rise and fall across the four days. The line joins the points in order.

import os
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"day": [1, 2, 3, 4], "units": [2, 1, 3, 2]})
df.plot(kind="line", x="day", y="units")
plt.savefig("chart.png")
print(os.path.getsize("chart.png"))

Dots with no joins

Each dot is one product. Its sideways spot is units, and its height is price.

import os
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"units": [2, 1, 3, 5], "price": [4.5, 6.0, 12.0, 2.0]})
df.plot(kind="scatter", x="units", y="price")
plt.savefig("chart.png")
print(os.path.getsize("chart.png"))

Two lines on one chart

A list of column names draws one line each. Both lines share the days.

import os
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"day": [1, 2, 3], "tea": [2, 4, 1], "coffee": [1, 2, 3]})
df.plot(kind="line", x="day", y=["tea", "coffee"])
plt.savefig("chart.png")
print(os.path.getsize("chart.png"))

Example

import io
import matplotlib.pyplot as plt
import os
import pandas as pd

SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

sales = pd.read_csv(io.StringIO(SALES_CSV))
print(sales.shape)
sales.plot(kind="scatter", x="units", y="price")
plt.savefig("chart.png")
print(os.path.getsize("chart.png"))

Key points

  • Use kind="line" to join points in order.
  • Use kind="scatter" for dots with no joins.
  • A scatter chart needs two number columns, one for each side.
  • A line chart suits values over time or in order.
  • A scatter chart suits two numbers measured together.

In this example, the sales table loads from its CSV text. shape prints first, with twelve rows and seven columns. The scatter chart puts units on one side and price on the other, with one dot per order. savefig writes the chart, and getsize prints the file size as proof.

Note

A scatter chart takes x and y as column names. A line chart takes the same two names.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which chart shows dots with no lines joining them?