pandas 如何在 Jupyter Notebook 中的绘图旁边显示数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45286696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:04:55  来源:igfitidea点击:

How to Display Dataframe next to Plot in Jupyter Notebook

pythonpython-2.7pandasmatplotlibjupyter-notebook

提问by David Yang

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:

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

enter image description here

在此处输入图片说明

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

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

Hereis 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

enter image description here

在此处输入图片说明

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!

谢谢你的想法!

采纳答案by Charles Parr

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:

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

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)

enter image description here

在此处输入图片说明

回答by Ezer K

Another possibility is to use the html to order things, following https://stackoverflow.com/a/44923103/4908900.

另一种可能性是使用 html 来订购东西,如下https://stackoverflow.com/a/44923103/4908900

Here is a working example, (there are probably more elegant ways to do it):

这是一个工作示例,(可能有更优雅的方法):

prefix = \
"""
 <!DOCTYPE html>
<html>
<head>
<style>
* {
    box-sizing: border-box;
}

.column {
    float: left;
    width: 33.33%;
    padding: 5px;
}

/* Clearfix (clear floats) */
.row::after {
    content: "";
    clear: both;
    display: table;
}
</style>
</head>
<body>

<h2>title</h2>

<div class="row">
  <div class="column">
"""

suffix = \
"""
  </div>
  <div class="column">
    <img src="pic_file.png" alt="Graph" style="width:100%">
  </div>
</div>
</body>
</html>
"""

df = pd.DataFrame(np.arange(36).reshape((6,6)),columns=['A','B','C','D','E','F'])
ax = df.plot(lw=4)
title = "mock data"
fig = ax.get_figure()
fig.savefig(title+".png")
html = prefix.replace('title', title)+df.to_html()+suffix.replace('pic_file.png', title+".png")
display_html(html, raw=True)

enter image description here

在此处输入图片说明

回答by HARSH MITTAL

You can use %matplotlib inline then just simply write the code ex df.head() plt.plot(df['X']) so %matplotlib inline will plot the data frame as well as the plot in one cell

您可以使用 %matplotlib inline 然后只需简单地编写代码 ex df.head() plt.plot(df['X']) 所以 %matplotlib inline 将绘制数据框以及一个单元格中的图