Would this work in 'Bar Plots & Scatter Plots: #9 Benchmarking Correlation'?

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:

  1. I set all 3 axes to share x-axis using sharex=True and set the figsize within plt.subplots() parameters.
  2. Used plt.setp() to set the properties of the Artist.
  3. Assigned norm_reviews['Fandango_Ratingvalue'] to a variable so I wouldn’t have to keep typing it.
  4. 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])

ax3.scatter(fandangorv, norm_reviews.iloc[:,3])


Thanks for the feedback!

Also, after a whole bunch of trial and error, I figured out a for loop to do the same thing, but I feel like it could also probably be improved?

Any tips/suggestions/help is very appreciated!

fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, figsize=(5, 10))

plt.setp((ax1, ax2, ax3), xlim=(0,5), ylim=(0,5), xlabel="Fandango")

x = norm_reviews['Fandango_Ratingvalue']
y_labels = ['Rotten Tomatoes', 'Metacritic', 'IMDB']
axes = [ax1, ax2, ax3]

for i in range(3):
    label = y_labels[i]
    col = 1 + i
    y = norm_reviews.iloc[:, col]
    axes[i].scatter(x, y)