如何调整plotly express面积图的大小?

2024-06-16 11:57:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Dash应用程序上创建了一个plotly express面积图。图表运行良好,但x轴上的第一个点和最后一个点似乎脱离了图表,只能看到一半(如附加屏幕截图所示)。我还尝试更改了graph的宽度和高度值,但似乎没有任何效果。 我是python和dash的新手,所以如果有任何帮助,我将不胜感激。我也在stack overflow上发布了我的第一个问题,所以如果我错过了任何东西或没有正确完成它,我将表示歉意

Area chart

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
import pyodbc
import dash_bootstrap_components as dbc
from datetime import datetime as dt
from app import app




page2 = html.Div(
    [

     
        dbc.Row(
            [
                dbc.Col(
                        # dbc.Card(
                                    html.Div(

                                        id="opportunity_heatmap",
                                        className="chart_div pretty_container",
                                        children = [
                                            dcc.Graph(id='my_graph',
                                                    
                                                      config={
                                                        'staticPlot': False,     # True, False
                                                        'scrollZoom': True,      # True, False
                                                        'doubleClick': 'reset',  # 'reset', 'autosize' or 'reset+autosize', False
                                                        'showTips': True,       # True, False
                                                        'displayModeBar': 'hover',  # True, False, 'hover'
                                                        'watermark': False,
                                                        # 'modeBarButtonsToRemove': ['pan2d','select2d'],
                                                    },
                                                )
                                            ]
                                    ),
                                # color="light", inverse=True),
                    md=12,
                ),
             
            ],
            no_gutters=True,
        ),

    
          

    ]
)




@app.callback(
    Output('my_graph','figure'),
    [Input('my_dropdown','value')]
)

def build_graph(trendGraph):

    dff=SQL_data
 



fig = px.area(dff, x="DeploymentDate", y="DeploymentID",  text='DeploymentID', color = 'DeploymentType' ,template='plotly_white'
                    ,category_orders={'DeploymentDate':['January-2019','February-2019','March-2019','April-2019','May-2019','June-2019','July-2019','August-2019','September-2019','October-2019','November-2019','December-2019','January-2020','February-2020','March-2020','April-2020','May-2020','June-2020','July-2020','August-2020','September-2020','October-2020','November-2020','December-2020','January-2021','February-2021']})

fig.update_layout(yaxis={'title':'No. of deployments'},
                    title={'text':'Deployments Trend',
                    'font':{'size':28},'x':0.5,'xanchor':'center'},
                    autosize=True,)
    
    return fig


def get_page2():
    return page2

Tags: fromimportfalsetrueappmyhtmlas
1条回答
网友
1楼 · 发布于 2024-06-16 11:57:01

在绘图上绘制的内容不能溢出网格布局,否则放大时,我们不仅会看到文本值,还会看到整个图形延伸到网格之外

显示这些值的唯一方法是缩小一点,这意味着相应地初始化图x_range。其思想是从相应的dataframe列中获取最小值和最大值,并根据需要扩展该范围

例如,此处添加4年允许完全显示y值(x轴上有年的一般用例,@seehttps://plotly.com/python/filled-area-plots/):

fig = px.area(
    df,
    x='year',
    y=y,
    color="continent",
    line_group="country",
    text=y,
    range_x=[df['year'].min()-2, df['year'].max()+2]
)

相关问题 更多 >