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

Pandas barh plot part of plot not showing

I am using the following code to generate a barh plot with data stored in a pandas dataframe. Problem is that part of the first bar is not showing and I’m not sure how to address that. Here is the code:

fig = plt.figure(figsize = (10, 6))
ax = fig.add_subplot()
ax2 = ax.twiny()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from io import StringIO
s = StringIO("""name     amount     price
A     40929   4066443
B     93904   9611272
C    188349  19360005
D    248438  24335536
E    205622  18888604
F    140173  12580900
G     76243   6751731
H     36859   3418329
I     29304   2758928
J     39768   3201269
K     30350   2867059""")
df = pd.read_csv(s, delimiter=' ', skipinitialspace=True)
df.plot(x = 'name', y = 'price', kind = 'barh', ax = ax, color = 'steelblue', width = 0.4, position = 0)
df.plot(x = 'name', y = 'amount', kind = 'barh', ax = ax2, color = 'salmon', width = 0.4, position = 1)

and here is how the plot looks like:

enter image description here

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

as you can see, part of plot for name K is not showing properly. How could I add some sort of a margin to create space between that axis and the plot itself?

>Solution :

One option is to adjust the upper y-limit:

ylim = ax.get_ylim()
ax.set_ylim((ylim[0], ylim[1]+0.4))

Output:
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