The initial code in the Learning Platform was feeling a bit repetitive to me, and I came across some useful methods in the pyplot docs, so I tried it out and it worked. But…
- Is there any benefit to keeping it as verbose as the initial code (other than seeing all the steps for the purpose of learning)?
- Do these shortcuts make this harder to read?
- Is there an even better/more efficient way to do this?
This is what I did:
- I set all 3 axes to share x-axis using
sharex=True
and set thefigsize
withinplt.subplots()
parameters. - Used
plt.setp()
to set the properties of the Artist. - Assigned
norm_reviews['Fandango_Ratingvalue']
to a variable so I wouldn’t have to keep typing it. - Used
.iloc
instead of typing the column names in full, as their names are in the next line of code as well.
import matplotlib.pyplot as plt
# generate 3 subplots and set parameters to share x-axis values
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, figsize=(5, 10))
# set xlim & ylim to (0, 5) and xlabel for all subplots
xlim = (0, 5)
ylim = (0, 5)
plt.setp((ax1, ax2, ax3), xlim=xlim, ylim=ylim, xlabel="Fandango")
fandangorv = norm_reviews['Fandango_Ratingvalue']
ax1.scatter(fandangorv, norm_reviews.iloc[:,1])
ax1.set(ylabel="Rotten Tomatoes")
ax2.scatter(fandangorv, norm_reviews.iloc[:,2])
ax2.set(ylabel="Metacritic")
ax3.scatter(fandangorv, norm_reviews.iloc[:,3])
ax3.set(ylabel="IMDB")
plt.show()
Thanks for the feedback!