citivin - 7 months ago 85

Python Question

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

Dataframe

`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.

`faces.plot(kind='bar').set_xticklabels(result[0]['scores'].keys())`

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 )

ax_r=faces.plot(kind='bar').set_xticklabels(result[0]['scores'].keys())

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

`ax_r.bar(...)`

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

Answer

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

calls.

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

Source (Stackoverflow)

Comments