2

I have a dataframe from Pandas

my_dataframe = pd.DataFrame({'a': range(5), 'b': range(20, 30, 2)})
my_dataframe
a   b
0   0   20
1   1   22
2   2   24
3   3   26
4   4   28
  • And a plot from Matplotlib
plt.bar(my_dataframe['a'].index, my_dataframe['a'].values)

enter image description here

What I wanna do is display both of them using IPython and don't losing the default df output format. The first solution of this link answared my question but don't have this format that I talked about. I want the following exemple in the same output cell:

enter image description here enter image description here

1 Answers1

2

Borrowing the CSS ordering directive from this SO post gives you a partial solution, but unfortunately the table is on the left and the plot is on the right.

import numpy as np
import pandas as pd   
from IPython.display import display, HTML
import matplotlib.pyplot as plt

CSS = """
.output {
    flex-direction: row;
}
"""

display(HTML('<style>{}</style>'.format(CSS)))

my_dataframe = pd.DataFrame({'a': range(5), 'b': range(20, 30, 2)})

plt.bar(my_dataframe['a'].index, my_dataframe['a'].values)
my_dataframe

Table and plot side by side

I believe the reason that the plot is rendered last, putting it to the right, is because of the %matplotlib inline directive that is part of most standard IPython configurations. If I understand correctly, the directive causes the plot to be output last, after the table is rendered.

howanu
  • 36
  • 3