Search code examples
pythonmatplotlib

Determine kind of Matplotlib Axes subplot


Given a matplotlib.axes_subplots.AexesSubplot object how do I tell what type of plot it contains? Is there a matplotlib feature that will determine this for me? for example...

I commonly plot data with pandas

import pandas as pd
df = pd.DataFrame({'y':range(10)})
line_ax = df.plot()

or

bar_ax = df.plot(kind='bar')

or

barh_ax = df.plot(kind='barh')

Solution

  • The matplotlib axes does not care about which plot it contains and it does not even know about it.
    The question would also be how to distinguish "kinds" of plots. What kind of plot is in an axes which contains 2 bars, several markers, 2 lines and 3 arrows?

    The kind argument to pandas plot function is simply a flag by which pandas decides which plotting function to call. This is independent of the axes and you may of course also have a plot produced by kind='bar' and kind='scatter' in the same axes.

    So the answer is: No there is no general way to determine the kind of plot in an axes, mainly due to the fact that there is no such thing as a "kind of plot".

    Of course, depending on what you'd need this type of information for, there are probably alternative ways to accomplish what you need.