1

I'm struggling to correctly apply Integerlabels to the AxesSubplot Object. Somehow all the other popular solutions don't work. I presume this has to do with the nature of how Pandas plots vs how Matplotlib natively plots a dataframe.

When looking at the docs, there is a mention about preventing resolution adjustments by using the optional parameter x_compat like so:

df['A'].plot(x_compat=True)

However this only throws me an error and I also can't find this parameter in the pandas 1.1.2 docs.

This is the code that produces the plot:

def count_id(id_val):
    plot = df[df['ID']==id_val]['TRANSCRIPTION_STRING'].value_counts().plot(kind='bar')
    plot.set_xticklabels(plot.get_xticklabels(), rotation=40, ha ='right')
    print(type(plot))

enter image description here

I have found this answer and it wasn't helpful for the following reasons:

  • It uses .gca() which throws an error for my case.
  • It's specifically using matplotlib instead of pandas.plot, which is the same under the hood, but hardly has very different documentation and isn't obvious to work for my case. More experienced users might differ.

It was a good suggestion and I see the similarities, but I very much tried to find it helpful before asking and it just wasn't helpful.

blkpingu
  • 1,556
  • 1
  • 18
  • 41

1 Answers1

1

You can use:

from matplotlib.ticker import MaxNLocator

plot.yaxis.set_major_locator(MaxNLocator(integer=True))

MaxNLocator is the default 'locator' for the ticks. You can also force some fixed multiples, e.g. set_major_locator(MultipleLocater(1)).

PS: The usual variable name for the return value of pandas plot functions is ax. This makes it clearer that the standard matplotlib functions can be used to customize the plots.

    ax = df[df['ID']==id_val]['TRANSCRIPTION_STRING'].value_counts().plot(kind='bar')
    ax.set_xticklabels(plot.get_xticklabels(), rotation=40, ha ='right')
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))

JohanC
  • 71,591
  • 8
  • 33
  • 66