citivin citivin - 1 year ago 152
Python Question

Plot pandas dataframe with varying number of columns along imshow

I want to plot an image and a pandas bar plot side by side in an iPython notebook. This is part of a function so that the dataframe containing the values for the bar chart can vary with respect to number of columns.

The libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline


faces = pd.Dataframe(...) # return values for 8 characteristics

This returns the the bar chart I'm looking for and works for a varying number of columns.


But I didn't find a way to plot it in a pyplot figure also containing the image. This is what I tried:

fig, (ax_l, ax_r) = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))
ax_l.imshow( img )

The output i get is the image on the left and an empty plot area with the correct plot below. There is

but I couldn't find a way around having to define the columns to be plotted.

Answer Source

You just need to specify your axes object in your DataFrame.plot calls.

In other words: faces.plot(kind='bar', ax=ax_r)