📜  Python Plotly:如何防止标题与情节重叠?

📅  最后修改于: 2022-05-13 01:55:39.450000             🧑  作者: Mango

Python Plotly:如何防止标题与情节重叠?

数据的结构和表示是数据驱动的决策过程以及整个数据科学路径的一个组成部分。数据视图是以图像和图像格式呈现数据的过程。数据的可视化表示使理解复杂概念变得更容易,并且可以轻松识别数据中的新模式。图形或图表格式的数据易于理解和分析。此类策略可以帮助您找到最佳的产品开发策略、业务增长指标和其他重要决策。好看的应用程序是无限的,可用于预测销售、股票价格分析、项目管理、网络流量监控等。它们只是数字技术如何通过帮助我们更好地理解它来影响我们作为您的汽车的日常生活的一个例子。 Plotly 的Python图形库可生成交互式、高质量的打印图形。如何创建线性部分、散点图、面积图、条形图、误差线、箱形部分、直方图、温度图、子图、多轴、白色图表和图表气泡的示例。

重叠标题示例

下面的Python代码为 2 个不同的类别创建条形图,沿 X 轴有 3 个元素。下面的代码生成一个输出图,其中标题与条形图重叠。我们可以通过多种方式手动减小它,例如增加图形大小或减小标题文本大小。但这些都是手动方法,因为不同的数据提供不同的 Bars,我们不能手动硬编码值。

Python3
import plotly.graph_objects as go
 
fig = go.Figure()
fig.add_trace(go.Bar(
    name='Set A',
    x=['Val x', 'Val y', 'Val z'], y=[6, 4, 3],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
fig.add_trace(go.Bar(
    name='Set B',
    x=['Val x', 'Val y', 'Val z'], y=[2, 9, 5],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
fig.update_layout(barmode='group',
                  title=dict(
                      text="Plotly 
Graph
Title Overlap",                       x=0.5,                       y=0.95,                       xanchor='center',                       yanchor='top',                       # pad = dict(                       #            t = 0                       #           ),                       font=dict(                                                     # family='Courier New, monospace',                           size=40,                           # color='#000000'                       )                   ))     fig.show()


Python3
import plotly.graph_objects as go
 
fig = go.Figure()
 
# Adding trace for data Set A
fig.add_trace(go.Bar(
    name='Set A',
    x=['Val x', 'Val y', 'Val z'], y=[6, 4, 3],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
# Adding trace for data Set B
fig.add_trace(go.Bar(
    name='Set B',
    x=['Val x', 'Val y', 'Val z'], y=[2, 9, 5],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
 
# Updating the layout of the figure
fig.update_layout(barmode='group',
                  title=dict(
                      text="Plotly 
Graph
Title Overlap",                       x=0.5,                       y=0.95,                       xanchor='center',                       yanchor='top',                       # pad = dict(                       #            t = 0                       #           ),                       font=dict(                           #family='Courier New, monospace',                           size=40,                           color='#000000'                       )                   ))   # Now we are setting a top margin of 200, which e # enables sufficient space between title and bar fig.update_layout(margin=dict(t=200)) fig.show()


输出:

解决方案 :

一种最佳解决方案是调整上边距填充。在下面的代码中,我们仅调整上边距,并将其他边距设置为默认值

Python3

import plotly.graph_objects as go
 
fig = go.Figure()
 
# Adding trace for data Set A
fig.add_trace(go.Bar(
    name='Set A',
    x=['Val x', 'Val y', 'Val z'], y=[6, 4, 3],
    error_y=dict(type='data', array=[1, 0.5, 1.5]),
    width=0.15
))
# Adding trace for data Set B
fig.add_trace(go.Bar(
    name='Set B',
    x=['Val x', 'Val y', 'Val z'], y=[2, 9, 5],
    error_y=dict(type='data', array=[0.5, 1, 2]),
    width=0.15
))
 
# Updating the layout of the figure
fig.update_layout(barmode='group',
                  title=dict(
                      text="Plotly 
Graph
Title Overlap",                       x=0.5,                       y=0.95,                       xanchor='center',                       yanchor='top',                       # pad = dict(                       #            t = 0                       #           ),                       font=dict(                           #family='Courier New, monospace',                           size=40,                           color='#000000'                       )                   ))   # Now we are setting a top margin of 200, which e # enables sufficient space between title and bar fig.update_layout(margin=dict(t=200)) fig.show()