简体   繁体   English

在 altair 图表中显示 B(Billion) 而不是 G(Giga) python

[英]To show B(Billion) instead of G(Giga) in altair charts python

Looking to replace G(Giga) with B(Billion) in a python code using altair charts.希望使用 altair 图表在 python 代码中用 B(Billion) 替换 G(Giga)。 I researched quite a lot on this but cannot find a way to override d3 format in python.我对此进行了大量研究,但找不到在 python 中覆盖 d3 格式的方法。 Currently using the below code for the axis format:当前使用以下代码作为轴格式:

line1=(alt.Chart(df_combined).mark_bar().encode(
     x=alt.X('Date:T', axis=alt.Axis(title=None, grid=False, format= '%b-%Y')),
     y = alt.Y(
         'Current Year:Q',
         axis= alt.Axis(
             title=None,
                titleAngle=0,
                titleY=-20,
                titleAlign="left",
                format='s',
                titleColor=COLORS_HEX['SMOKEY_BLUE']),
              ),
    color = alt.value(COLORS_HEX['SMOKEY_BLUE'])
).add_selection(sel_selection).transform_filter(sel_selection)
)

however, this produces the axis and tool tip with SI notification which shows 10^9 as G and not B(billion).但是,这会生成带有 SI 通知的轴和工具提示,其中将 10^9 显示为 G 而不是 B(十亿)。 Lot of topics on this related subject shows to achieve this in javascript by overriding the d3 format function but how do I override this function by calling it in python notebook.关于这个相关主题的很多主题都表明通过覆盖 d3 格式函数在 javascript 中实现这一点,但是我如何通过在 python notebook 中调用它来覆盖这个函数。 Would be very helpful to get some pointers on this topic.获得有关此主题的一些指示将非常有帮助。

Thank you.谢谢你。

I don't know of any way to create custom d3 formatters from Python within the standard Altair display mechanisms.我不知道有什么方法可以在标准 Altair 显示机制中从 Python 创建自定义 d3 格式化程序。 You can always export your chart to JSON and then use vega-embed to manually display your chart with whatever Javascript extras you wish.您始终可以将图表导出为 JSON,然后使用vega-embed手动显示您的图表以及您希望的任何 Javascript 附加功能。

From the Python side, one way to accomplish roughly what you want is to use labelExpr to define a custom label expression.在 Python 方面,大致完成您想要的一种方法是使用labelExpr定义自定义标签表达式。 For example:例如:

import pandas as pd
import altair as alt
df = pd.DataFrame({'x': [1E9, 2E9, 3E9]})
alt.Chart(df).mark_point().encode(
    alt.X('x', axis=alt.Axis(tickCount=5, labelExpr='datum.value / 1E9 + "B"'))
)

在此处输入图片说明

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

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