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()
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")