简体   繁体   English

如何避免 Python 中 Altair 图中的文本重叠

[英]How to avoid overlapping texts in Altair plots in Python

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

The link here describes the label transformation in javascript but I want adapt it for python Online Altair documentation on label transformation I have tried to include.transform(avoidMarks = ["Force5", "Force6"]) to "text" section but it shows error as undefined object not callable. The link here describes the label transformation in javascript but I want adapt it for python Online Altair documentation on label transformation I have tried to include.transform(avoidMarks = ["Force5", "Force6"]) to "text" section but it shows错误为 undefined object not callable。 I request you to kindly help me on how to use lable transform to avoid the overlapping text.我请求您帮助我了解如何使用标签转换来避免文本重叠。 Thank you very much.非常感谢。

This was just added to VegaLite and won't be in Altair until the next release of Vega-Lite and Altair is upgraded to support the latest version of Vega-Lite.这只是添加到 VegaLite中,在下一个 Vega-Lite 版本和 Altair 升级以支持最新版本的 Vega-Lite 之前不会出现在 Altair 中。

  {
    "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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM