Mathew K Analytics

Lesson 22 · Data visualisation in python

Creating Interactive Dashboards by Combining Multiple Plots for Effective Data Visualization

We saw how to: Load real-world data Make different plots Arrange plots as side by side or grids Combine different datasets Label, annotate, and save…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb

Recap: What did we learn?#

We saw how to:

  • Load real-world data
  • Make different plots
  • Arrange plots as side by side or grids
  • Combine different datasets
  • Label, annotate, and save dashboards

With these skills, you can tell many data stories!

print("Daily temperature data loaded:", df_temp.shape) df_temp.head()

plt.subplot(1, 2, 1) plt.plot(df["Month"], df["Passengers"], color="blue") plt.title("Passenger trend") plt.xlabel("Month") plt.ylabel("Passengers") plt.xticks(rotation=45)

plt.subplot(1, 2, 2) plt.hist(df["Passengers"], bins=16, color="green", alpha=0.7) plt.title("Distribution of passengers") plt.xlabel("Passengers")

plt.tight_layout() plt.show() print("Rows, columns:", df.shape) df.head() We will build simple dashboards step by step in this lesson.

For this lesson, we will use pandas and matplotlib for data and plotting#

import pandas as pd import matplotlib.pyplot as plt

Plots will show inside the notebook#

%matplotlib inline

Let us check what our raw data looks like in a line plot.#

plt.figure(figsize=(8, 4)) plt.plot(df["Month"], df["Passengers"], label="Monthly airline passengers") plt.title("Monthly airline passengers over time") plt.xlabel("Month") plt.ylabel("Passengers") plt.xticks(rotation=45)

Ready for more? Try creating your own dashboards with other datasets!#

If you enjoyed this tutorial, please like and subscribe to our channel for more lessons.

Thank you for learning with us!

plt.show()

Build a dashboard combining both datasets.#

fig, axs = plt.subplots(1, 2, figsize=(14, 4))

axs[0].plot(df["Month"], df["Passengers"], color="navy") axs[0].set_title("Airline Passengers") axs[0].set_xlabel("Month") axs[0].set_ylabel("Passengers") axs[0].tick_params(axis='x', rotation=45)

axs[1].plot(df_temp["Date"][:365], df_temp["Temp"][:365], color="crimson") axs[1].set_title("Daily Minimum Temperatures (first year)") axs[1].set_xlabel("Day") axs[1].set_ylabel("Temperature (C)") axs[1].tick_params(axis='x', rotation=45)

fig.suptitle("Passengers and Weather Dashboard", fontsize=16) plt.tight_layout(rect=[0,0,1,0.95]) plt.show()

plt.title("Passenger number spread") plt.xlabel("Passengers")

plt.tight_layout() plt.show()

We can add even more plots: a grid 2 by 2 for a richer dashboard.#

fig, axs = plt.subplots(2, 2, figsize=(12, 8))

axs[0, 0].plot(df["Month"], df["Passengers"], color='mediumslateblue') axs[0, 0].set_title("Trend over time")

axs[0, 1].hist(df["Passengers"], bins=16, color='orange', alpha=0.7) axs[0, 1].set_title("Histogram")

axs[1, 0].boxplot(df["Passengers"]) axs[1, 0].set_title("Boxplot")

axs[1, 1].plot(df["Passengers"].rolling(12).mean(), color='darkgreen') axs[1, 1].set_title("12 Month Average")

plt.tight_layout() plt.show()

Let us try adding titles and a main title to our dashboard.#

fig, axs = plt.subplots(2, 2, figsize=(12, 8))

fig.suptitle("Airline Passengers Dashboard", fontsize=16)

axs[0, 0].plot(df["Passengers"], color='navy') axs[0, 0].set_title("Monthly Trend")

axs[0, 1].hist(df["Passengers"], color='tomato', bins=12) axs[0, 1].set_title("Distribution")

axs[1, 0].scatter(df.index, df["Passengers"], color='purple', s=12) axs[1, 0].set_title("Passengers (scatter)")

axs[1, 1].plot(df["Passengers"].diff(), color='slategrey')

Dashboard tip: Use clear legends and labels.#

You can use the legend method to label lines and add gridlines for better reading.#

plt.figure(figsize=(8, 5)) plt.plot(df["Month"], df["Passengers"], label="Airline Passengers", color='blue') plt.plot(df["Month"], pd.Series(df["Passengers"]).rolling(12).mean(), label="12-month Avg", color='gold', linestyle='--') plt.title("Passengers with Rolling Average") plt.xlabel("Month") plt.ylabel("Count") plt.xticks(rotation=45) plt.legend(loc="upper left") plt.grid(True, alpha=0.3) plt.tight_layout() plt.show()

ax.set_ylabel("")

fig.suptitle("Dashboard with Auto Spacing", fontsize=15)

axs[0, 0].plot(df["Passengers"], color='grey') axs[0, 0].set_title("Trend")

axs[0, 1].hist(df["Passengers"], color='lime', bins=10) axs[0, 1].set_title("Histogram")

axs[1, 0].boxplot(df["Passengers"]) axs[1, 0].set_title("Boxplot")

axs[1, 1].plot(df["Passengers"].rolling(6).mean(), color='orange') axs[1, 1].set_title("6-month Average")

plt.tight_layout(rect=[0,0,1,0.95]) plt.show()

Practice: Time to create your own dashboard layout.#

num_plots = int(input("How many plots do you want (1 to 4)? ")) if num_plots < 1 or num_plots > 4: print("Please pick between 1 and 4.") else: fig, axs = plt.subplots(1, num_plots, figsize=(4*num_plots, 4)) if num_plots == 1: axs = [axs] if num_plots >= 1: axs[0].plot(df["Passengers"], color='brown') axs[0].set_title("Trend") if num_plots >= 2: axs[1].hist(df["Passengers"], color='purple') axs[1].set_title("Histogram") if num_plots >= 3: axs[2].boxplot(df["Passengers"]) axs[2].set_title("Boxplot") if num_plots == 4: axs[3].plot(df["Passengers"].diff(), color='dimgray') axs[3].set_title("Change") plt.tight_layout() plt.show()

Challenge: Make a dashboard with three rows -- one for each plot type.#

fig, axs = plt.subplots(3, 1, figsize=(8, 12))

axs[0].plot(df["Month"], df["Passengers"], color='slateblue') axs[0].set_title("Time series")

axs[1].hist(df["Passengers"], bins=14, color='deepskyblue') axs[1].set_title("Histogram")

axs[2].boxplot(df["Passengers"], vert=False, patch_artist=True, boxprops=dict(facecolor='gold', color='black')) axs[2].set_title("Boxplot")

plt.tight_layout() plt.show()

You can add annotations to highlight interesting points.#

fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(df["Passengers"], color='midnightblue') ax.set_title("Key Points Highlighted") max_idx = df["Passengers"].idxmax() ax.annotate("Highest", xy=(max_idx, df["Passengers"].iloc[max_idx]), xytext=(max_idx, df["Passengers"].iloc[max_idx]+30), arrowprops=dict(arrowstyle="->", color='red')) plt.tight_layout() plt.show()

Dashboard troubleshooting: If plots do not appear, check if plt.show() was called.#

plt.plot([1, 2, 3], [3, 2, 1])

plt.show() # Uncomment this line if plots do not appear.#

Pro tip: Save your dashboard to a file with savefig.#

fig, axs = plt.subplots(1, 2, figsize=(10, 4)) axs[0].plot(df["Passengers"], color='orangered') axs[1].hist(df["Passengers"], color='royalblue') plt.tight_layout() fig.savefig("my_dashboard.png") print("Dashboard saved as my_dashboard.png")

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.