Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with y-ticks alignment in pandas bar chart

here is the data and code that I’m using to plot the bar chart as shown in the output plot. The issue is with y-ticks, I need to have y-ticks aligned in a vertical manner such that they cover/coincides with all four bars. If we use rot = ‘some int value’ the ticks rotate, but they do not exactly align vertically covering four bars.

Polymer Depth Residual time
HPAM7030 50 1159
HPAM7030 100 1638
HPAM7030 200 2170
HPAM7030 500 2718
APAM7525 50 2040
APAM7525 100 3101
APAM7525 200 4176
APAM7525 500 5270
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(['science', 'notebook', 'grid'])
df.pivot(index = 'Polymer', columns = 'Depth', values = 'Residual time').plot(kind='barh')
plt.title('Residual time of different polymers (2 year simulation)')
plt.ylabel("Type of Polymer")
plt.xlabel("Residual time (Days)")
plt.show()

output plot

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I find it easier to use the Axes object returned by the pandas plotting method and set the y tick label alignment to 'center':

ax = df.pivot(index = 'Polymer', columns = 'Depth', values = 'Residual time').plot(kind='barh', rot=90)
ax.set_title('Residual time of different polymers (2 year simulation)')
ax.set(ylabel="Type of Polymer",xlabel="Residual time (Days)")
ax.set_yticklabels(ax.get_yticklabels(), va="center")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading