【问题标题】:How to avoid overlapping texts in Altair plots in Python如何避免 Python 中 Altair 图中的文本重叠
【发布时间】:2021-08-10 12:13:19
【问题描述】:
import altair as alt
import pandas as pd

# assign data of lists.  
ProxyData = {'Separation Mechanism': ['Force1', 'Force2', 'Force3', 'Force4', 'Force5', 
'Force6' ], 'Parm1':[1,100,2.5,38,20,30], 'Parm2':[300,1000,150,1500,1000,1300], 'Parm3': 
[0.1,30,7.5,1,30,270],'Parm4':[81650,9000,2000,4000,2000,1540] }  

# Create DataFrame  
df3 = pd.DataFrame(ProxyData)  

 base =alt.Chart(df3).mark_bar(opacity=0.5).encode(
  alt.X('Parm1', scale=alt.Scale(type='log'), axis = alt.Axis(title='Particle')),
  x2='Parm2',
  y=alt.Y('Parm4',sort = None, scale=alt.Scale(type='log'), axis = alt.Axis(title='Rate')),
  y2 = 'Parm3'
  ).properties(width = 500, height = 400
  )
  bars = base.mark_bar(opacity=0.5).encode(
   color= alt.Color('Separation Mechanism', scale=alt.Scale(scheme='accent'))
  )

text = base.mark_text(
align='right',
baseline='middle',
color='black',
dx=100, dy =3, fontSize = 14).encode(text='Separation Mechanism')
bars+text

此处的链接描述了 javascript 中的标签转换,但我想将其改编为 python Online Altair documentation on label transformation 我试图将 .transform(avoidMarks = ["Force5", "Force6"]) 包含到“text”部分,但它显示错误,因为未定义的对象不可调用。 我请求您帮助我了解如何使用标签转换来避免文本重叠。非常感谢。

【问题讨论】:

    标签: python label bar-chart altair vega-lite


    【解决方案1】:

    This was just added to VegaLite 在下一个 Vega-Lite 版本和 Altair 升级以支持最新版本的 Vega-Lite 之前不会出现在 Altair 中。

    【讨论】:

      【解决方案2】:
        {
          "config": {"view": {"continuousWidth": 400, "continuousHeight": 300}},
           "layer": [
         {
        "mark": {"type": "bar", "opacity": 0.5},
        "encoding": {
          "color": {
            "type": "nominal",
            "field": "Separation Mechanism",
            "scale": {"scheme": "accent"}
          },
          "x": {
            "type": "quantitative",
            "axis": {"title": "Particle"},
            "field": "Parm1",
            "scale": {"type": "log"}
          },
          "x2": {"field": "Parm2"},
          "y": {
            "type": "quantitative",
            "axis": {"title": "Rate"},
            "field": "Parm4",
            "scale": {"type": "log"},
            "sort": null
          },
          "y2": {"field": "Parm3"}
        },
        "height": 400,
        "width": 500
        },
        {
        "mark": {
          "type": "text",
          "align": "right",
          "baseline": "middle",
          "color": "black",
          "dx": 100,
          "dy": 3,
          "fontSize": 14
        },
        "encoding": {
          "text": {"type": "nominal", "field": "Separation Mechanism"},
          "x": {
            "type": "quantitative",
            "axis": {"title": "Particle"},
            "field": "Parm1",
            "scale": {"type": "log"}
          },
          "x2": {"field": "Parm2"},
          "y": {
            "type": "quantitative",
            "axis": {"title": "Rate"},
            "field": "Parm4",
            "scale": {"type": "log"},
            "sort": null
          },
          "y2": {"field": "Parm3"}
        },
        "height": 400,
        "width": 500
      }
       ],
       "data": {"name": "data-4b7e34321793962372af06f754b1c5ca"},
       "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json",
       "datasets": {
        "data-4b7e34321793962372af06f754b1c5ca": [
        {
          "Separation Mechanism": "Force1",
          "Parm1": 1,
          "Parm2": 300,
          "Parm3": 0.1,
          "Parm4": 81650
        },
        {
          "Separation Mechanism": "Force2",
          "Parm1": 100,
          "Parm2": 1000,
          "Parm3": 30,
          "Parm4": 9000
        },
        {
          "Separation Mechanism": "Force3",
          "Parm1": 2.5,
          "Parm2": 150,
          "Parm3": 7.5,
          "Parm4": 2000
        },
        {
          "Separation Mechanism": "Force4",
          "Parm1": 38,
          "Parm2": 1500,
          "Parm3": 1,
          "Parm4": 4000
        },
        {
          "Separation Mechanism": "Force5",
          "Parm1": 20,
          "Parm2": 1000,
          "Parm3": 30,
          "Parm4": 2000
        },
        {
          "Separation Mechanism": "Force6",
          "Parm1": 30,
          "Parm2": 1300,
          "Parm3": 270,
          "Parm4": 1540
        }
        ]
        }
        }
      
      
      
      
        Hi @joelostblom, thank you very much for your reply. How to add label transform function to get rid off overlapping labels.
      

      【讨论】:

        猜你喜欢
        • 2021-07-14
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 2022-10-20
        • 2023-03-21
        • 1970-01-01
        • 2019-12-07
        • 2019-12-21
        相关资源
        最近更新 更多