Prepare Data for Python Line Charts
Learn how to structure and understand data for line charts in Python, including x-axis and y-axis values, monthly trends, and beginner-friendly plotting examples.
1. Understand the Dataset
Before creating a chart, we need to understand the data.
We will use a university student enrollment dataset.
Sample Dataset: Monthly Student Enrollment
| Month | Students Enrolled |
|---|---|
| January | 50 |
| February | 65 |
| March | 60 |
| April | 80 |
| May | 75 |
| June | 100 |
| July | 110 |
| August | 95 |
| September | 120 |
| October | 130 |
| November | 125 |
| December | 150 |
Understand the rows and columns
Consider the following example:
| Month | Students Enrolled |
|---|---|
| January | 50 |
| February | 65 |
- Rows: Each row represents one month's enrollment information.
- Columns: Each column represents a type of information.
- Category: Month is the category used to organize the records.
- Numerical value: Students Enrolled represents the number of students.
Understand the X-axis and Y-axis
A line chart normally has two axes.
X-axis
Horizontal axis
Month
January, February, March, and so on.
Y-axis
Vertical axis
Number of Students
50, 65, 60, 80, and so on.
How do values become points?
Each month has a corresponding enrollment value.
For example:
| Month | Students | Chart point |
|---|---|---|
| January | 50 | January → 50 |
| February | 65 | February → 65 |
| March | 60 | March → 60 |
| April | 80 | April → 80 |
The chart places each point according to its month and numerical value.
Then, the points are connected with lines.
Important: The X-axis shows the order or position of the data, while the Y-axis shows the corresponding numerical values.
6. When to Use This Chart
When should we use a line chart?
Use a line chart when:
- You want to show changes over time.
- Your data has a meaningful order.
- You want to identify trends.
- You want to compare changes in multiple datasets.
- You want to identify fluctuations or sudden changes.
Suitable data for line charts
- Daily temperature.
- Monthly sales.
- Annual population.
- Weekly attendance.
- Quarterly revenue.
- Monthly student enrollment.
Importance of ordered data
Line charts work best when the X-axis follows a meaningful sequence.
For example:
January → February → March → April
This order makes sense because months follow a natural sequence.
However, consider these unrelated categories:
Computer Science → English → Physics → Biology
Connecting these subjects with a line may suggest a continuous progression that does not actually exist.
A bar chart would usually be more appropriate for comparing unrelated subjects.
When NOT to use a line chart
Avoid line charts when:
- You are comparing unrelated categories.
- The data has no meaningful order.
- Connecting the points would create a misleading relationship.
- You have too many lines and the chart becomes difficult to read.
- You only need to compare individual values.
Line Chart vs Bar Chart vs Area Chart vs Scatter Plot
| Feature | Line Chart | Bar Chart | Area Chart | Scatter Plot |
|---|---|---|---|---|
| Purpose | Show trends | Compare values | Show trends and magnitude | Show relationships |
| Suitable data | Ordered data | Categories | Ordered data | Paired numerical values |
| Best use case | Monthly sales | Sales by product | Population growth over time | Height vs weight |
| Main difference | Connects points | Uses rectangular bars | Fills area below line | Uses unconnected points |
Practical examples:
- Line chart: Monthly sales from January to December.
- Bar chart: Sales of different products.
- Area chart: Total revenue growth over time with emphasis on magnitude.
- Scatter plot: Relationship between study hours and examination marks.
7. Chart Components
A line chart contains several important components.
Visual illustration
Explanation of each component
| Component | Explanation |
|---|---|
| Chart Title | Describes what the chart represents. |
| X-axis | Horizontal axis, usually representing time or ordered categories. |
| Y-axis | Vertical axis, representing numerical values. |
| X-axis Values | Values displayed along the horizontal axis. |
| Y-axis Values | Numerical scale displayed along the vertical axis. |
| Data Points | Individual points representing observations. |
| Connecting Lines | Lines joining data points. |
| Axis Labels | Names describing each axis. |
| Tick Marks | Small marks showing positions on an axis. |
| Gridlines | Reference lines that help read values. |
| Legend | Identifies different lines when multiple datasets are displayed. |
Understand different trends
Increasing Trend
Values move upward.
Decreasing Trend
Values move downward.
Constant Trend
Values remain unchanged.
Fluctuating Trend
Values repeatedly rise and fall.
Multiple lines on one chart
A multiple line chart displays two or more datasets on the same chart.
For example, we can compare monthly enrollment in:
- Computer Science.
- Information Technology.
Each department gets a separate line and a legend identifies each line.
Remember: Use different colors or markers to make multiple lines easy to distinguish.