首页 > 解决方案 > 在绘图中从底部到图形留出空间

问题描述

我正在使用此代码在一个图中绘制两个图,但我不知道如何从底部给图形留出空间。

def visualize_trends(df, features, titles, fig_title, fn, vs = 0.18, h = 1000, w = 800):

    trace0 = go.Scatter(
        x = df[features[0]],
        y = df['Past_Developers_Count'],
        mode = 'lines+markers',
        name = 'Past',
    )

    trace1 = go.Scatter(
        x = df[features[1]],
        y = df['Future_Developers_Count'],
        mode = 'lines+markers',
        name = 'Future',
    )

    fig = tools.make_subplots(rows = 2, cols = 1, vertical_spacing = vs, subplot_titles = titles)

    fig.append_trace(trace0, 1, 1)
    fig.append_trace(trace1, 2, 1)

    fig['layout'].update(height = h, width = w, paper_bgcolor = 'rgb(233,233,233)', title = fig_title)

    py.iplot(fig, filename = fn)

输出:

在此处输入图像描述

是否有任何参数可以提供空间,就像我vertical_spacing以前在两个图表之间提供空间一样?

标签: pythonplotly

解决方案


I figured it out myself. You can give margin to give required space. In margin, you can specify space from all sides left, right, bottom, up.

margin = dict(l = 10, r = 20, b = 30, u = 40)

You can pass values according to your own requirements. I did like this in my case to give space from bottom,

margin = dict(b = 140)

Complete function,

def visualize_trends(df, features, titles, fig_title, fn, vs = 0.18, h = 1000, w = 800):

    trace0 = go.Scatter(
        x = df[features[0]],
        y = df['Past_Developers_Count'],
        mode = 'lines+markers',
        name = 'Past',
    )

    trace1 = go.Scatter(
        x = df[features[1]],
        y = df['Future_Developers_Count'],
        mode = 'lines+markers',
        name = 'Future',
    )

    fig = tools.make_subplots(rows = 2, cols = 1, vertical_spacing = vs, subplot_titles = titles)

    fig.append_trace(trace0, 1, 1)
    fig.append_trace(trace1, 2, 1)

    fig['layout'].update(height = h, width = w, margin = dict(b = 140), 
                         paper_bgcolor = 'rgb(233,233,233)', title = fig_title)

    py.iplot(fig, filename = fn)

Output:

enter image description here

Note: I am posting this answer so someone can save their time on this issue.


推荐阅读