Subplot Syntax
We can create subplots using .subplot()
.
The command plt.subplot()
needs three arguments to be passed into it:
- The number of rows of subplots
- The number of columns of subplots
- The index of the subplot we want to create
For instance, the command plt.subplot(2, 3, 4)
would create “Subplot 4” from the figure above.
Any plt.plot()
that comes after plt.subplot()
will create a line plot in the specified subplot. For instance:
# Data sets
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
# First Subplot
plt.subplot(1, 2, 1)
plt.plot(x, y, color='green')
plt.title('First Subplot')
# Second Subplot
plt.subplot(1, 2, 2)
plt.plot(x, y, color='steelblue')
plt.title('Second Subplot')
Example
months = range(12)temperature = [36, 36, 39, 52, 61, 72, 77, 75, 68, 57, 48, 48]flights_to_hawaii = [1200, 1300, 1100, 1450, 850, 750, 400, 450, 400, 860, 990, 1000]
plt.subplot(1,2,1) plt.plot(months,temperature)plt.subplot(1,2,2) plt.plot(temperature, flights_to_hawaii, "o")plt.show()# Display both subplots
plt.subplot(2,1,1)plt.plot(straight_line,x)plt.subplot(2, 2, 3)plt.plot(x,parabola)plt.subplot(2,2,4)plt.plot(x,cubic)plt.subplots_adjust(bottom=0.2, wspace=0.35)plt.show()
AX AND SET XTICKS
plt.xlabel("Months")plt.ylabel("Conversion")
plt.plot(months, conversion)
# Your work hereax = plt.subplot()ax.set_xticks(months)ax.set_xticklabels(month_names)ax.set_yticks([0.10, 0.25, 0.5, 0.75])ax.set_yticklabels(["10%", "25%", "50%", "75%"])
plt.show()
Comments
Post a Comment