Python:matplotlib/pandas - 将数据框绘制为子图中的表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49201194/
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 05:17:54  来源:igfitidea点击:

Python: matplotlib/pandas - Plotting a dataframe as a table in a subplot

pythonpandasdataframematplotlibsubplot

提问by McNibbler

I have a (very simplified) segment of code here for an open source project I'm working on for plotting ATE results from STDF data...

我在这里有一个(非常简化的)代码段,用于我正在处理的一个开源项目,用于从 STDF 数据绘制 ATE 结果......

def plot_everything_from_one_test(test_data, data, num_of_sites, test_tuple):

    plt.figure()


    # Title for everything
    plt.suptitle("Test Information Goes Here")

    # Find the limits
    low_lim = get_plot_min(data, test_tuple, num_of_sites)
    hi_lim = get_plot_max(data, test_tuple, num_of_sites)

    ######        

    table = function_that_returns_a_dataframe()

    plt.subplot(121)

    # HOW WOULD I ACTUALLY PUT A DATAFRAME HERE

    ######

    # Plots the trendline
    plt.subplot(222)
    plot_full_test_trend(test_data, low_lim, hi_lim)
    plt.title("Trendline")

    # Plots the histogram
    plt.subplot(224)
    plot_full_test_hist(test_data, low_lim, hi_lim)
    plt.title("Histogram")

    plt.show()

Basically, all I really want to do is in subplot section (121) to render simply a table containing the contents of an array or dataframe or whatever. This dataframe happens to be 10 columns wide and indefinitely long (in the order of magnitude of ~10s, but can be trimmed if needed). I've looked around and I know matplotlib has table(), but from what I can see it creates a new figure (unwanted) and it also tries to force a graph to render on top of it, even though I really don't want to have that render because it really would be useless. I saw this could be sort-of circumvented, but I don't know how I would manage it while preserving my subplots. Perhaps I just don't understand how matplotlib works with them.

基本上,我真正想做的就是在子图部分 (121) 中简单地呈现一个包含数组或数据框内容的表格或其他内容。这个数据框恰好有 10 列宽和无限长(大约 10 秒的数量级,但可以根据需要进行修剪)。我环顾四周,我知道 matplotlib 有 table(),但是从我所看到的,它创建了一个新图形(不需要的),并且它还试图强制图形在其上呈现,即使我真的没有想要渲染,因为它真的没用。我看到这可以被规避,但我不知道如何在保留我的次要情节的同时管理它。也许我只是不明白 matplotlib 如何与它们一起工作。

Another problem is that when I can sort-of get tables to render, they are absurdly SLOW, reaching upwards of a minute to render a 10x30-something dataframe. Also, the text renders absurdly too small and doesn't scale. Sorry if this all sounds like a cluster of problems, but I really don't know how I would deal with what should be a simple render of a table. I may just convert to a string if I really need and render it as text. Thanks in advance!

另一个问题是,当我可以某种程度地获取要呈现的表格时,它们慢得荒谬,达到一分钟以上才能呈现 10x30 大小的数据帧。此外,文本渲染得太小而且无法缩放。对不起,如果这一切听起来像是一堆问题,但我真的不知道我将如何处理应该是简单的表格渲染。如果我真的需要,我可能只是将其转换为字符串并将其呈现为文本。提前致谢!

回答by Simon

You can add a tableto a subplot - you dont need a separate figure for it. Then you can turn the axis off for that subplot and it wont try to render anything other than the table in that region

您可以将 a 添加table到子图 - 您不需要单独的图形。然后您可以关闭该子图的轴,它不会尝试渲染该区域中除表格以外的任何内容

You also mentioned that your code is running slow when trying to render a 10x30 dataframe in the subplot. There might be an issue with you implementation of the table because the following renders within a second:

您还提到尝试在子图中渲染 10x30 数据帧时,您的代码运行缓慢。表格的实现可能存在问题,因为以下内容会在一秒钟内呈现:

import matplotlib.pyplot as plt
import pandas as pd

d = {'x{}'.format(i): range(30) for i in range(10)}

table = pd.DataFrame(d)

plt.figure()

# table
plt.subplot(121)

cell_text = []
for row in range(len(table)):
    cell_text.append(table.iloc[row])

plt.table(cellText=cell_text, colLabels=table.columns, loc='center')
plt.axis('off')

# plot
plt.subplot(222)
plt.plot(table['x1'],table['x2'])

# plot
plt.subplot(224)
plt.plot(table['x1'],table['x2'])

plt.show()

enter image description here

在此处输入图片说明