Skip to content

Instantly share code, notes, and snippets.

@joyadauche
Last active April 30, 2020 16:28
Show Gist options
  • Save joyadauche/a044c680338e0752fa12b71c6aedc480 to your computer and use it in GitHub Desktop.
Save joyadauche/a044c680338e0752fa12b71c6aedc480 to your computer and use it in GitHub Desktop.
The Data Viz Wiz: Introducing Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots();
plt.show()
# equivalents to above - different ways of creating an axes
# 1a -
# ax = plt.subplot()
# plt.show()
# 1b -
# fig = plt.figure()
# ax = fig.add_subplot()
# plt.show()
# 1c -
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1)
# plt.show()
# 2a -
# ax = plt.subplots()
# plt.show()
# 2b -
# fig = plt.figure()
# ax = fig.subplots()
# plt.show()
# 2c -
# fig = plt.figure()
# ax = fig.subplots(1, 1)
# plt.show()
# 2d -
# fig = plt.figure()
# ax = fig.subplots(nrows=1, ncols=1)
# plt.show()
# 3a -
# ax = plt.axes()
# plt.show()
# 3b -
# ax = plt.axes([0.1, 0.1, 0.8, 0.8])
# plt.show()
# 3c -
# fig = plt.figure()
# ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# plt.show()
import matplotlib.pyplot as plt
import pandas as pd
lagos_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[40.2, 41.9, 47.0, 50.1, 57.9, 62.5, 63.8, 61.9, 54.3, 47.4, 40.5, 42.8]
}
lagos_average_monthly_temperature = pd.DataFrame(lagos_average_monthly_temperature_dict)
fig, ax = plt.subplots();
ax.plot(lagos_average_monthly_temperature['month'], lagos_average_monthly_temperature['temperature'])
plt.show()

lagos_monthly_average_temperature table

month temperature
Jan 40.2
Feb 41.9
Mar 47.0
Apr 50.1
May 57.9
Jun 62.5
Jul 63.8
Aug 61.9
Sep 54.8
Oct 47.4
Nov 40.5
Dec 42.8
import matplotlib.pyplot as plt
import pandas as pd
lagos_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[40.2, 41.9, 47.0, 50.1, 57.9, 62.5, 63.8, 61.9, 54.3, 47.4, 40.5, 42.8]
}
abuja_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[42.2, 43.9, 48.0, 52.1, 59.9, 64.5, 66.8, 63.9, 50.3, 49.4, 42.5, 44.8]
}
lagos_average_monthly_temperature = pd.DataFrame(lagos_average_monthly_temperature_dict)
abuja_average_monthly_temperature = pd.DataFrame(abuja_average_monthly_temperature_dict)
fig, ax = plt.subplots();
ax.plot(lagos_average_monthly_temperature['month'], lagos_average_monthly_temperature['temperature'])
ax.plot(abuja_average_monthly_temperature['month'], abuja_average_monthly_temperature['temperature'])
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
lagos_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[40.2, 41.9, 47.0, 50.1, 57.9, 62.5, 63.8, 61.9, 54.3, 47.4, 40.5, 42.8]
}
lagos_average_monthly_temperature = pd.DataFrame(lagos_average_monthly_temperature_dict)
fig, ax = plt.subplots();
ax.plot(lagos_average_monthly_temperature['month'], lagos_average_monthly_temperature['temperature'],
marker="8", markersize=8, markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue",
linestyle=":", linewidth=2, color="red")
ax.set_xlabel("Date (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
ax.set_title("Temperature Trends in Lagos")
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
lagos_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[40.2, 41.9, 47.0, 50.1, 57.9, 62.5, 63.8, 61.9, 54.3, 47.4, 40.5, 42.8]
}
abuja_average_monthly_temperature_dict = {
'month':['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
'temperature':[42.2, 43.9, 48.0, 52.1, 59.9, 64.5, 66.8, 63.9, 50.3, 49.4, 42.5, 44.8]
}
lagos_average_monthly_temperature = pd.DataFrame(lagos_average_monthly_temperature_dict)
abuja_average_monthly_temperature = pd.DataFrame(abuja_average_monthly_temperature_dict)
fig, ax = plt.subplots();
ax.plot(lagos_average_monthly_temperature['month'], lagos_average_monthly_temperature['temperature'],
marker="8", markersize=8, markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue",
linestyle=":", color='red', label="Lagos")
ax.plot(abuja_average_monthly_temperature['month'], abuja_average_monthly_temperature['temperature'],
marker=".", markersize=8, markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue",
color="darkgreen", label="Abuja")
ax.set_xlabel("Date (months)")
ax.set_ylabel("Average temperature (Fahrenheit degrees)")
ax.set_title("Temperature Patterns across Cities")
ax.legend()
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
lagos_average_monthly_temperature_dict = {
'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'temperature': [40.2, 41.9, 47.0, 50.1, 57.9, 62.5, 63.8, 61.9, 54.3, 47.4, 40.5, 42.8]
}
abuja_average_monthly_temperature_dict = {
'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'temperature': [42.2, 43.9, 48.0, 52.1, 59.9, 64.5, 66.8, 63.9, 50.3, 49.4, 42.5, 44.8]
}
lagos_average_monthly_temperature = pd.DataFrame(lagos_average_monthly_temperature_dict)
abuja_average_monthly_temperature = pd.DataFrame(abuja_average_monthly_temperature_dict)
fig, ax = plt.subplots(2, 1, sharey=True)
ax[0].plot(lagos_average_monthly_temperature['month'], lagos_average_monthly_temperature['temperature'])
ax[1].plot(abuja_average_monthly_temperature['month'], abuja_average_monthly_temperature['temperature'], color="r")
ax[0].set_ylabel("Average temperature")
ax[1].set_ylabel("Average temperature")
ax[1].set_xlabel("Date (months)")
ax[0].set_title("Lagos average temperature (Fahrenheit degrees)")
ax[1].set_title("Abuja average temperature (Fahrenheit degrees)")
plt.tight_layout() # same as fig.tight_layout()
plt.show()
# Examples where the ax object is an array of axes object
# 1a -
# fig = plt.figure()
# ax = fig.subplots(2, 2)
# ax[0, 0].plot()
# ax[0, 1].plot()
# ax[1, 0].plot()
# ax[1, 1].plot()
# plt.show()
# 1b -
# fig = plt.figure()
# ax = fig.subplots(2, 2)
# ax.flat[0].plot()
# ax.flat[1].plot()
# ax.flat[2].plot()
# ax.flat[3].plot()
# plt.show()
# 1c -
# fig = plt.figure()
# ((ax1, ax2), (ax3, ax4)) = fig.subplots(2, 2)
# ax1.plot()
# ax2.plot()
# ax3.plot()
# ax4.plot()
# plt.show()
# 2 -
# fig = plt.figure()
# ax1 = fig.add_subplot(221)
# ax2 = fig.add_subplot(222)
# ax3 = fig.add_subplot(223)
# ax4 = fig.add_subplot(224)
# plt.show()
# 3 -
# fig = plt.figure()
# ax1 = fig.add_axes([0.1, 0.6, 0.3, 0.3])
# ax2 = fig.add_axes([0.6, 0.6, 0.3, 0.3])
# ax3 = fig.add_axes([0.1, 0.1, 0.3, 0.3])
# ax4 = fig.add_axes([0.6, 0.1, 0.3, 0.3])
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment