Home > Net >  Python Matplotlib: subplot configuration passing data-frame data to specific position
Python Matplotlib: subplot configuration passing data-frame data to specific position

Time:04-26

I am trying to plot a bar chart using Matplotlib's subplot function, below are my attempts:

Note: x-axis is categorical data, y-axis is numeric df['Analysis'].value_counts().plot(kind = 'bar')

This plots a bar graph with the correct numeric data associated with each category. This syntax does not work for matplotlib subplot feature b/c it does not have an associated df function:

axs[0,0].df['Analysis'].value_counts().plot(kind = 'bar')

returns

AttributeError: 'AxesSubplot' object has no attribute 'df'

I tried using pandas library functions the following way but now the numeric y-axis data does not correspond to the categorical

   fig, axs = plt.subplots(3, 3, figsize = (16,14))

   x = df['Analysis'].unique()
   y = df['Analysis'].value_counts()
   axs[0,0].bar(x,y)

How can I plot the data as a bar plot in subplots such that the numeric data corresponds to the correct category?

CodePudding user response:

In the pandas plotting methods, you can specify which ax to plot on:

fig, axs = plt.subplots(3, 3, figsize = (16,14))

df['Analysis'].value_counts().plot(kind='bar', ax=axs[0,0])
  • Related