我正在尝试尽可能多地使用 Altair 重现此图表。 https://fivethirtyeight.com/wp-content/uploads/2014/04/hickey-bechdel-11.png?w=575
我坚持让黑线划分通过/失败。这类似于这个 Altair 示例:https : //altair-viz.github.io/gallery/step_chart.html。但是:在 538 viz 中,最终日期的值必须扩展到最后一个元素的整个宽度。在步骤图示例和我的解决方案中,只要遇到最后一个日期元素,该行就会停止。
我查看了 altair 的 github 和 google 组,没有发现与此问题类似的内容。
import altair as alt
import pandas as pd
movies=pd.read_csv('https://raw.githubusercontent.com/fivethirtyeight/data/master/bechdel/movies.csv')
domain = ['ok', 'dubious','men', 'notalk', 'nowomen']
base=alt.Chart(movies).encode(
alt.X("year:N",bin=alt.BinParams(step=5,extent=[1970,2015]),axis=alt.Axis(labelAngle=0, labelLimit=50,labelFontSize=8),title=None), alt.Y("count()",stack='normalize',title=None,axis=alt.Axis(format='%',values=[0, 0.25,0.50,0.75,1]))
).properties(width=400)
main=base.transform_calculate(cleanrank='datum.clean_test == "ok" ? 1 : datum.clean_test == "dubious" ? 2 : datum.clean_test == "men" ? 3 : datum.clean_test == "notalk" ? 4 : 5'
).mark_bar(stroke='white' #add horizontal lines
).encode(
alt.Color("clean_test:N",scale=alt.Scale(
domain=domain,
range=['dodgerblue', 'skyblue', 'pink', 'coral','red']))
,order=alt.Order('cleanrank:O', sort='ascending') …Run Code Online (Sandbox Code Playgroud)