Skip to main content
 首页 » 编程设计

python之如何在 plotly 中切割子图底部的大空间

2024年12月31日14zengkefu

我正在使用 plotly 生成交互式绘图。在我的例子中,我将在一个图中有一个表和一个图。

import matplotlib 
matplotlib.use('Agg') 
import plotly.offline as offline 
from plotly import tools 
import plotly.graph_objs as go 
 
trace1=go.Scatter(x=[1, 2, 3], y=[1, 3, 5], name='abc', mode = 'lines+markers', xaxis='x1', yaxis='y1', showlegend=True) 
trace2=go.Scatter(x=[1, 2, 3], y=[1, 2, 3], name='cba', mode = 'lines+markers', xaxis='x1', yaxis='y1', showlegend=True) 
trace3=go.Table( 
        domain=dict(x=[0, 1], y=[0, 0.5]), 
        header=dict(values=['', 'fields1', 'fields2', 'fields3']), 
        cells=dict(values=[[['row1'], ['row2'], ['row3']], [['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]], font = dict(color = '#506784', size = 11)) 
        ) 
axis=dict( 
    showline=True, 
    zeroline=False, 
    showgrid=True, 
    mirror=True, 
    ticklen=4, 
    gridcolor='#ffffff', 
    tickfont=dict(size=10) 
) 
 
param1 = dict( 
    xaxis1=dict(axis, **dict(domain=[0, 1], anchor='y1', showticklabels=False)), 
    yaxis1=dict(axis, **dict(domain=[0.55, 1], anchor='x1', tickprefix='$', hoverformat='.2f')), 
        ) 
layout1 = dict( 
    title='Bitcoin mining stats for 180 days', 
    margin = dict(t=100), 
    showlegend=False, 
    plot_bgcolor='rgba(228, 222, 249, 0.65)' 
) 
layout1.update(param1) 
fig1=dict(data=[trace1, trace2, trace3], layout=layout1) 
offline.plot(fig1, auto_open=False, output_type='file', filename='abc.html') 

如何删除表格下方的空间?是否可以根据元素的实际大小自动改变fiture的大小?

请您参考如下方法:

查看您的 domain 参数,尤其是您设置 yaxis 的地方:

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

和

yaxis1=dict(axis, **dict(domain=[0.55, 1], 

根据代码,您说表格将在 yaxis 上的 0% 到 50% 之间绘制。和图表 - 在 55% 到 100% 之间。但看!你的 table 太小,无法填补这个空缺 (50%)。

所以你必须有选项来解决这个问题:

  1. 增加表格中的数据量。然后表的大小增加,间隙会减小,然后消失;

  2. 或者将表格转到底部并增加图形大小(例如 0-15% 到表格,15-100% 到 yaxis 图形),结果应该是这样的(下面的完整代码):

代码:

import matplotlib 
matplotlib.use('Agg') 
import plotly.offline as offline 
from plotly import tools 
import plotly.graph_objs as go 
 
trace1=go.Scatter(x=[1, 2, 3], y=[1, 3, 5], name='abc', mode = 'lines+markers', xaxis='x1', yaxis='y1', showlegend=True) 
trace2=go.Scatter(x=[1, 2, 3], y=[1, 2, 3], name='cba', mode = 'lines+markers', xaxis='x1', yaxis='y1', showlegend=True) 
trace3=go.Table( 
        #Set space for table from 0% to 15% 
        domain=dict(x=[0, 1], y=[0, 0.15]), 
        header=dict(values=['', 'fields1', 'fields2', 'fields3']), 
        cells=dict(values=[[['row1'], ['row2'], ['row3']], [['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]], font = dict(color = '#506784', size = 11)) 
        ) 
axis=dict( 
    showline=True, 
    zeroline=False, 
    showgrid=True, 
    mirror=True, 
    ticklen=4, 
    gridcolor='#ffffff', 
    tickfont=dict(size=10) 
) 
 
param1 = dict( 
    xaxis1=dict(axis, **dict(domain=[0, 1], anchor='y1', showticklabels=False)), 
    #Set space to graph from 15% to 100% 
    yaxis1=dict(axis, **dict(domain=[0.15, 1], anchor='x1', tickprefix='$', hoverformat='.2f')), 
        ) 
layout1 = dict( 
    title='Bitcoin mining stats for 180 days', 
    margin = dict(t=100), 
    showlegend=False, 
    plot_bgcolor='rgba(228, 222, 249, 0.65)', 
    autosize=True, 
) 
layout1.update(param1) 
fig1=dict(data=[trace1, trace2, trace3], layout=layout1) 
offline.plot(fig1, auto_open=False, output_type='file', filename='abc.html') 

您还可以设置 xaxis 参数以获得更漂亮的绘图(将绘图空间从 25% 设置为 75%):