Python 牵牛星山脊线图中的不同颜色
这是我之前关于牵牛星山脊线图的延伸 我有这样一个情节:Python 牵牛星山脊线图中的不同颜色,python,altair,vega-lite,ridgeline-plot,Python,Altair,Vega Lite,Ridgeline Plot,这是我之前关于牵牛星山脊线图的延伸 我有这样一个情节: import pandas as np import numpy as np source = pd.DataFrame(columns=list('ab')) source['a'] = np.random.randint(0,17,size=500) source['color'] = source['a'].apply(lambda x: 'blue' if x < 10 else 'red'] source['a'] =
import pandas as np
import numpy as np
source = pd.DataFrame(columns=list('ab'))
source['a'] = np.random.randint(0,17,size=500)
source['color'] = source['a'].apply(lambda x: 'blue' if x < 10 else 'red']
source['a'] = source['a'].astype('str')
source['b'] = np.random.randint(1000,5000,size=500).astype('float')
import altair as alt
step = 20
overlap = 1
alt.Chart(source, height=step).transform_joinaggregate(
mean_temp='mean(b)', groupby=['a']
).transform_bin(
['bin_max', 'bin_min'], 'b'
).transform_aggregate(
value='count()', groupby=['a', 'b', 'bin_min', 'bin_max']
).transform_impute(
impute='value', groupby=['a', 'b'], key='bin_min', value=0
).mark_area(
interpolate='monotone',
fillOpacity=0.8,
stroke='lightgray',
strokeWidth=0.5
).encode(
alt.X('bin_min:Q', bin='binned', title=''),
alt.Y(
'value:Q',
scale=alt.Scale(range=[step, -step * overlap]),
axis=None
),
alt.Fill(
'b:Q',
legend=None,
)
).facet(
row=alt.Row(
'a:N',
title=None,
header=alt.Header(labelAngle=0, labelAlign='right')
)
).properties(
title='',
bounds='flush'
).configure_facet(
spacing=0
).configure_view(
stroke=None
).configure_title(
anchor='end'
)
将熊猫作为np导入
将numpy作为np导入
source=pd.DataFrame(columns=list('ab'))
source['a']=np.random.randint(0,17,size=500)
source['color']=source['a']。应用(λx:如果x<10,则为蓝色,否则为红色)]
source['a']=source['a'].astype('str')
source['b']=np.random.randint(10005000,size=500.astype('float'))
将牵牛星导入为alt
步骤=20
重叠=1
alt.Chart(源,高度=步长)。转换(
平均值(b),分组比=['a']
).转换(u bin)(
['bin_max'、'bin_min']、'b'
).转换集合(
value='count()',groupby=['a','b','bin\u min','bin\u max']
).转换输入(
插补='value',分组依据=['a','b'],键='bin\u min',值=0
).马丘区(
插值='单调',
填充不透明度=0.8,
“浅灰色”,
冲程宽度=0.5
).编码(
alt.X('bin_min:Q',bin='binned',title=''),
替代Y(
'值:Q',
比例=高度比例(范围=[步长,-步长*重叠],
轴=无
),
替换填充(
“b:Q”,
图例=无,
)
).小平面(
row=alt.row(
“a:N”,
标题=无,
header=alt.header(labelagle=0,labelAlign='right')
)
).物业(
标题=“”,
“齐平”
).configure_facet(
间距=0
).configure\u视图(
冲程=无
).配置标题(
锚='end'
)
我的问题是如何使绘图的行具有不同的颜色(“蓝色”或“红色”取决于数据框中的“颜色”列)?我尝试使用
alt.Scale(domain='color:N')
中的alt.Fill()
,color='color:N'
参数encode()
但它不起作用。刻面标题标签也应该是彩色的。您可以通过使用
也就是说,您已经基于“b”
列设置了颜色编码,因此需要对该信息进行不同的编码;例如,可以使用不透明度
下面是一个将这些内容组合在一起的示例:
将熊猫作为np导入
将numpy作为np导入
source=pd.DataFrame(columns=list('ab'))
source['a']=np.random.randint(0,17,size=500)
源['color']=源['a']。应用(λx:如果x<10,则为蓝色,否则为红色)
source['a']=source['a'].astype('str')
source['b']=np.random.randint(10005000,size=500.astype('float'))
将牵牛星导入为alt
步骤=20
重叠=1
alt.Chart(源,高度=步长)。转换(
平均值(b),分组比=['a']
).转换(u bin)(
['bin_max'、'bin_min']、'b'
).转换集合(
value='count()',groupby=['a','b','bin\u min','bin\u max','color']
).转换输入(
插补='value',分组依据=['a','b','color'],键='bin\u min',值=0
).马丘区(
插值='单调',
填充不透明度=0.8,
“浅灰色”,
冲程宽度=0.5
).编码(
alt.X('bin_min:Q',bin='binned',title=''),
替代Y(
'值:Q',
比例=高度比例(范围=[步长,-步长*重叠],
轴=无
),
alt.Fill('颜色:N',比例=无),
高度不透明度(
“b:Q”,
图例=无,
)
).小平面(
row=alt.row(
“a:N”,
标题=无,
header=alt.header(labelagle=0,labelAlign='right')
)
).物业(
标题=“”,
“齐平”
).configure_facet(
间距=0
).configure\u视图(
冲程=无
).配置标题(
锚='end'
)
你又在救我了。谢谢!