如何将图表和表组合成一个Plotly图像

时间:2018-01-12 15:52:02

标签: python bar-chart plotly

我已成功创建了一个带有以下代码片段的阴谋条形图图像:

Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt.
You must not let any exception whatsoever propagate through Qt code.
If that is not possible, in Qt 5 you must at least reimplement
QCoreApplication::notify() and catch all exceptions there.

(process:25567): GLib-CRITICAL **: g_source_unref_internal: assertion 'source != NULL' failed

并使用以下代码片段成功创建了第二个图表表格图像:

chart = go.Figure(data=chart_matrix)

现在我想将2张图片合并为1张(即条形图下面有桌子),但我似乎无法实现这一点。我尝试过使用子图,但是情节文档说没有将Plotly Table插入Subplot的本地方法。其他文档指定使用布局与轴和域相结合,我不明白。

有人可以分享一个如何做到这一点的简单示例吗?感谢

2 个答案:

答案 0 :(得分:2)

通过tutorial for table subplots,我能够复制你需要的东西!

可悲的是,我们不能像你说的那样使用Subplots,但是这个方法并不复杂,我会尽力解释它,以便你可以为其他用例实现它。

您需要知道属性domain,我们可以在这里指定包含x and y start and end values的字典,这样我们就可以将图形放置在不同的位置,这个概念就是用来创建副区。

注意:域x和y零值从画布的左下角开始。

因此,对于该表,我们可以看到域名如下。

domain=dict(x=[0, 1], y=[0, 0.3])

正如你所看到的那样,我们说它应该从左端开始并延伸到情节画布的右端,但是如果你看y值,我们说它应该从底部开始并且只伸展到0.3(30%)的阴影画布,因此也有足够的空间来容纳条形图!

对于条形图,使用不同的方法指定域,问题是我们没有方便的domain参数,因此我们可以使用x and y axis来定位它。

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[4, 5, 6],
    xaxis="X1",
    yaxis="Y1"
)

layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))

在这里我们可以看到,在条形图定义中,我们只需要设置图表的x和y轴的名称,在布局中我们可以定义类似于我们在表中所做的域。

如果您遇到任何问题并且问题已完全解决,请与我们联系!

以下是供您参考的工作代码段!

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly import tools
import pandas as pd
import numpy as np
from datetime import datetime
init_notebook_mode(connected=True)

trace = go.Table(
    header=dict(values=['A Scores', 'B Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),
    domain=dict(x=[0, 1],
                y=[0, 0.3]))

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[4, 5, 6],
    xaxis="X1",
    yaxis="Y1"
)

layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))
fig = go.Figure(data = [trace,trace1], layout = layout)
iplot(fig, filename = 'basic_table')

答案 1 :(得分:0)

import plotly.graph_objs as go

trace = go.Table(
    header=dict(values=['A Scores', 'B Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),
    domain=dict(x=[0, 1],
                y=[0, 0.3])
)

trace1 = go.Table(
    header=dict(values=['C Scores', 'D Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),

)


layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))



fig = go.Figure(data = [trace,trace1], layout = layout)
fig.show()