gpt4 book ai didi

python - Altair条形图具有可变宽度的条形?

转载 作者:行者123 更新时间:2023-12-03 16:22:23 25 4
gpt4 key购买 nike

我正在尝试在 Python 中使用 Altair 制作一个条形图,其中条形的宽度取决于源数据框的一列中的数据。最终目标是得到这样的图表:

A bar chart with bars of variable width

条形的高度对应于每种能源技术的边际成本(在源数据框中以列形式给出)。条形宽度对应于每种能源技术的容量(也在源数据框中以列的形式给出)。颜色也是来自源数据框的序数数据。条形按边际成本的升序排序。 (这样的情节在能源行业被称为“发电堆栈”)。这在 matplotlib 中很容易实现,如下面的代码所示:

import matplotlib.pyplot as plt 

# Make fake dataset
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')

# Choose the width of each bar and their positions
width = [0.1,0.2,3,1.5,0.3]
y_pos = [0,0.3,2,4.5,5.5]

# Make the plot
plt.bar(y_pos, height, width=width)
plt.xticks(y_pos, bars)
plt.show()

(代码来自 https://python-graph-gallery.com/5-control-width-and-space-in-barplots/ )

但是有没有办法用 Altair 做到这一点?我想用 Altair 来做这件事,所以我仍然可以获得 Altair 的其他强大功能,比如工具提示、选择器/绑定(bind),因为我有很多其他数据想要在条形图旁边显示。

我的源数据的前 20 行如下所示:

enter image description here

(与上面显示的图表不完全匹配)。

最佳答案

在 Altair 中,执行此操作的方法是使用 rect明确地标记和构建你的酒吧。这是一个模仿您的数据的示例:

import altair as alt
import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame({
'MarginalCost': 100 * np.random.rand(30),
'Capacity': 10 * np.random.rand(30),
'Technology': np.random.choice(['SOLAR', 'THERMAL', 'WIND', 'GAS'], 30)
})

df = df.sort_values('MarginalCost')
df['x1'] = df['Capacity'].cumsum()
df['x0'] = df['x1'].shift(fill_value=0)

alt.Chart(df).mark_rect().encode(
x=alt.X('x0:Q', title='Capacity'),
x2='x1',
y=alt.Y('MarginalCost:Q', title='Marginal Cost'),
color='Technology:N',
tooltip=["Technology", "Capacity", "MarginalCost"]
)

enter image description here

要在不预处理数据的情况下获得相同的结果,可以使用 Altair 的转换语法:
df = pd.DataFrame({
'MarginalCost': 100 * np.random.rand(30),
'Capacity': 10 * np.random.rand(30),
'Technology': np.random.choice(['SOLAR', 'THERMAL', 'WIND', 'GAS'], 30)
})

alt.Chart(df).transform_window(
x1='sum(Capacity)',
sort=[alt.SortField('MarginalCost')]
).transform_calculate(
x0='datum.x1 - datum.Capacity'
).mark_rect().encode(
x=alt.X('x0:Q', title='Capacity'),
x2='x1',
y=alt.Y('MarginalCost:Q', title='Marginal Cost'),
color='Technology:N',
tooltip=["Technology", "Capacity", "MarginalCost"]
)

关于python - Altair条形图具有可变宽度的条形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com