【问题标题】:How to change the background color of labels or otherwise make them more readable?如何更改标签的背景颜色或以其他方式使它们更具可读性?
【发布时间】:2021-11-14 02:32:41
【问题描述】:

我使用 plotly express 并希望为我的文本添加背景颜色以提高可读性。这是我的代码:

fig = px.parallel_categories(df,
                             dimensions=["batch_size", "out_channels", "num_layers",
                                         "aggr", "bias", "binned_" + continous_col],
                             color=continous_col,
                             color_continuous_scale= px.colors.sequential.Rainbow,
                             labels={"batch_size": "Batch Größe",
                                     "out_channels": "n",
                                     "num_layers": "Sequenzlänge L",
                                     "aggr": "Aggregatsfunktion",
                                     "bias": "Bias",
                                     "binned_" + continous_col: "TPR"}
                )
fig.show()

结果如下:

由于所有颜色,阅读情节中的数字或单词有点困难。我希望它们显示在白色背景上。或者我也可以想象使文本更大或更粗。你知道我是怎么做到的吗?

【问题讨论】:

    标签: python plot plotly


    【解决方案1】:

    背景颜色没什么可做的,因为px.parallel_categories() 尺寸的刻度标签似乎没有背景属性:

    'tickfont': {'color': '#2a3f5f', 'family': '"Open Sans", verdana, arial, sans-serif', 'size': 10}
    

    情节 1

    为了让之前的图更易读,你可以通过以下方式调整字体的大小:

    fig.data[0].tickfont.size = 24
    

    情节 2

    并没有直接的方法可以做到这一点,但以下 sn-p 会将所有刻度标签设置为 bold 字体:

    for d in fig.data[0].dimensions:
        d.values = ['<b>'+str(e)+'</b>' for e in d.values] 
    fig.show()
    

    情节 3

    完整代码:

    import plotly.express as px
    
    df = px.data.tips()
    fig = px.parallel_categories(df)
    f = fig.full_figure_for_development(warn=False)
    # fig.data[0].labelfont.size = 24
    fig.data[0].tickfont.size = 24
    
    for d in fig.data[0].dimensions:
        d.values = ['<b>'+str(e)+'</b>' for e in d.values] 
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多