Plotting
pandas draws charts with matplotlib, behind a single method: .plot(). In CodeMIRAI, the chart appears under the code.
A line chart
import pandas as pd
import matplotlib.pyplot as plt
monthly = pd.DataFrame({
"revenue": [12.0, 13.5, 15.2, 14.8, 17.1, 19.4],
"costs": [ 9.5, 10.1, 10.8, 11.0, 11.6, 12.3],
}, index=["Jan", "Feb", "Mar", "Apr", "May", "Jun"])
monthly.plot(title="Revenue and costs", ylabel="lakh rupees", marker="o")
plt.tight_layout()
plt.show()Each column becomes a line, and the index runs along the x-axis. plt.show() displays the chart; tight_layout() stops labels being cut off.
Bar charts
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.Series({"Python": 340, "JavaScript": 290, "SQL": 210, "Java": 180, "React": 150})
sales.plot(kind="bar", color="#5a48f5", title="Enrolments by course", rot=0)
plt.tight_layout()
plt.show()
sales.sort_values().plot(kind="barh", color="#0aa9cf", title="Sorted, horizontal")
plt.tight_layout()
plt.show()Use barh when the labels are long, and sort the values first so the chart reads in order. rot=0 keeps the x-axis labels upright.
Histograms
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
rng = np.random.default_rng(1)
scores = pd.Series(rng.normal(70, 12, size=500).clip(0, 100))
scores.plot(kind="hist", bins=20, color="#5a48f5", edgecolor="white", title="Exam scores")
plt.xlabel("score")
plt.tight_layout()
plt.show()A histogram shows how values are distributed: where they cluster, how widely they spread, and whether they lean to one side.
Scatter plots
import pandas as pd
import matplotlib.pyplot as plt
study = pd.DataFrame({
"hours": [1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7, 8, 9],
"score": [52, 55, 61, 58, 66, 70, 68, 75, 79, 77, 85, 90],
})
study.plot(kind="scatter", x="hours", y="score", s=60, color="#e0446a", title="Study time and score")
plt.tight_layout()
plt.show()
print("correlation:", round(study["hours"].corr(study["score"]), 2))A scatter plot shows whether two measurements move together. The correlation, between -1 and 1, puts a number on it: close to 1 means a strong rising relationship.
Pie charts
import pandas as pd
import matplotlib.pyplot as plt
devices = pd.Series({"Phone": 58, "Laptop": 31, "Tablet": 11})
devices.plot(kind="pie", autopct="%.0f%%", startangle=90, title="Visitors by device")
plt.ylabel("")
plt.tight_layout()
plt.show()Pie charts only work for a few parts of one whole. With more than four or five slices, a bar chart is easier to read.
Several charts side by side
import pandas as pd
import matplotlib.pyplot as plt
monthly = pd.DataFrame({
"visitors": [1200, 1350, 1600, 1580, 1900, 2300],
"signups": [ 60, 72, 95, 90, 120, 150],
}, index=["Jan", "Feb", "Mar", "Apr", "May", "Jun"])
monthly.plot(subplots=True, layout=(1, 2), figsize=(9, 3), marker="o", legend=False, title=["Visitors", "Sign-ups"])
plt.tight_layout()
plt.show()subplots=True gives each column its own chart, which helps when the columns have very different scales. Here, sign-ups would be a flat line along the bottom of a shared chart.