如何在Jupyter Notebook中的图旁边显示数据框 [英] How to Display Dataframe next to Plot in Jupyter Notebook

查看:1020
本文介绍了如何在Jupyter Notebook中的图旁边显示数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何在Jupyter Notebook中(水平)相邻(水平)显示两个图,但是我不知道是否有一种方法可以显示旁边带有数据框的图.我想它看起来可能像这样:

I understand how to display two plots next to each other (horizontally) in Jupyter Notebook, but I don't know if there is a way to display a plot with a dataframe next to it. I imagine it could look something like this:

但是,我无法做到这一点,每当我打印出数据框时,它就会出现在我的绘图下方...

However, I'm not able to do this, and whenever I print out the dataframe, it appears below my plot...

此处是一个类似的问题,但是我也在要垂直定位的同一单元内输出图.

Here is a similar question, but I am also outputting plots within this same cell that I want to be vertically oriented.

我目前有这个:

# line plots
df_plot[['DGO %chg','DLM %chg']].plot(figsize=(15,5),grid=True)
plt.ylim((-ylim,ylim))

df_plot[['Diff']].plot(kind='area',color='lightgrey',figsize=(15,1))
plt.xticks([])
plt.xlabel('')
plt.ylim((0,ylim_diff))
plt.show()

# scatter plots
plt.scatter(x=df_scat[:-7]['DGO'],y=df_scat[:-7]['DLM'])
plt.scatter(x=df_scat[-7:]['DGO'],y=df_scat[-7:]['DLM'],color='red')
plt.title('%s Cluster Last 7 Days'%asset)
plt.show()

# display dataframe
# display(df_scat[['DGO','DLM']][:10]) <-- prints underneath, not working

红色框显示我希望数据框出现的位置.有人对如何执行此操作有任何想法吗?

Where the red box shows where I want my dataframe to appear. Does anyone have any ideas about how to do this?

感谢您的想法!

推荐答案

我不知道如何控制DataFrame直接显示的位置-但是我过去使用的一种解决方法是渲染将DataFrame用作matplotlib表,然后其行为应与任何其他matplotlib图相同.您可以使用:

I'm not aware of how to control the location of where the DataFrame will display directly - but one work around I have used in the past is to render the DataFrame as a matplotlib table and then it should behave like any other matplotlib plot. You can use:

import matplotlib.pyplot as plt
from matplotlib import six
import pandas as pd
import numpy as np

df = pd.DataFrame()
df['x'] = np.arange(0,11)
df['y'] = df['x']*2

fig = plt.figure(figsize=(8,5))

ax1 = fig.add_subplot(121)
ax1.scatter(x=df['x'],y=df['y'])

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

这篇关于如何在Jupyter Notebook中的图旁边显示数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆