4

The following code generates a description of my dataset, plus a histogram, one on top of the other...

get_ipython().magic(u'matplotlib inline')
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(dfx.hits, bins=20)

dfx.hits.describe()

(And not in the order that I'd expect.)

Here's the result:

Description and histogram

But I want these figures side by side. Is this possible?

samthebrand
  • 3,020
  • 7
  • 41
  • 56

2 Answers2

3

James' suggestion to use subplots is helpful, but it's not as detailed as the answer provided at this question:

How to Display Dataframe next to Plot in Jupyter Notebook

Charles Parr's answer there helped me write this code:

fig = plt.figure(figsize=(10,4))

ax1 = fig.add_subplot(121)
ax1.hist(dfx.hits, bins=20, color='black')
ax1.set_title('hits x users histogram')

dfxx = dfx.hits.describe().to_frame().round(2)

ax2 = fig.add_subplot(122)
font_size=12
bbox=[0, 0, 1, 1]
ax2.axis('off')
mpl_table = ax2.table(cellText = dfxx.values, rowLabels = dfxx.index, bbox=bbox, colLabels=dfxx.columns)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)

Which generated side-by-side subplots of a histogram and a description:

histogram and description side-by-side with python

samthebrand
  • 3,020
  • 7
  • 41
  • 56
2

You're going to want to use matplotlib's built-in sub-plotting to make a (2,1) plot and place the first plot at (0,0) and the second at (1,0). You can find the official documentation here which isn't great.

I'd recommend checking out the guide at python-course.eu here:

Also the reason its out of order is because you're not calling plt.show() or the seaborn equivalent and hence ipython is spitting the graph out last as it is called without being assigned somewhere else.

Similar to if you had:

import matplotlib.pyplot as plt

plt.plot(range(10),range(10))
print("hello")

The output is:

>>hello
>>'Some Graph here'