Text Labels Coordinates Discrepancy (Adding Y-tick Labels)

Screen Link:

My Code:

style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize=(9, 5))
ax.barh(white_corr.index, white_corr, left=2, height=0.5)
ax.barh(red_corr.index, red_corr, height=0.5, left=-0.1)

ax.grid(b=False)
ax.set_yticklabels([])
ax.set_xticklabels([])

x_coords = {'Alcohol': 0.82, 'Sulphates': 0.77, 'pH': 0.91,
            'Density': 0.80, 'Total Sulfur Dioxide': 0.59,
            'Free Sulfur Dioxide': 0.6, 'Chlorides': 0.77,
            'Residual Sugar': 0.67, 'Citric Acid': 0.76,
            'Volatile Acidity': 0.67, 'Fixed Acidity': 0.71}
y_coord = 9.8
for y_label, x_coord in x_coords.items():
    ax.text(x_coord, y_coord, y_label)
    y_coord -= 1

ax.axvline(0.5, c='grey', alpha=0.6, linewidth=1, ymin=0.1, ymax=0.9)
ax.axvline(1.45, c='grey', alpha=0.6, linewidth=1, ymin=0.1, ymax=0.9)

plt.show()

From the tutorial, the coordinate for the y-tick label is 9.8, and it reduces by 1 for every y-tick label. This is understood.

To visually separate the labels from the bar plots, we added two vertical lines:

The ymax coordinate of the vertical lines is 0.9 (based on the axvline code) . This is confusing spatially.

I would have expected the ymax to be closer to 9.8, since the Alcohol tick label has a y-coordinate of 9.8.

Please, I need clarification on this.

Thank you.

Hi @michaelaigboks

According to the documentation ymin and ymax “Should be between 0 and 1, 0 being the bottom of the plot, 1 the top of the plot.”

In that order of ideas, the reason why the line is 0.9 is to leave a small space between the line and the border of the plot. It’s a different scale and it can be confusing, but it’s also one of the reasons why we need to read the documentation of each library that we use

Good luck!!

2 Likes

@alegiraldo666

Thank you so much for clearing this up, it makes so much sense now.

1 Like