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

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

问题描述

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

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

红色框显示我希望数据框出现的位置.有没有人对如何做到这一点有任何想法?

感谢您的意见!

解决方案

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

将 matplotlib.pyplot 导入为 plt从 matplotlib 导入六将熊猫导入为 pd将 numpy 导入为 npdf = pd.DataFrame()df['x'] = np.arange(0,11)df['y'] = df['x']*2fig = plt.figure(figsize=(8,5))ax1 = fig.add_subplot(121)ax1.scatter(x=df['x'],y=df['y'])ax2 = fig.add_subplot(122)字体大小=14bbox=[0, 0, 1, 1]ax2.axis('关闭')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)

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.

I currently have this:

# 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?

Thanks for your thoughts!

解决方案

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天全站免登陆