Drawing line plot for a histogram

喜你入骨 提交于 2020-03-01 03:46:11

问题


I'm trying to reproduce this chart using Altair as much as I can. https://fivethirtyeight.com/wp-content/uploads/2014/04/hickey-bechdel-11.png?w=575

I'm stuck at getting the black line dividing pass/fail. This is similar to this Altair example: https://altair-viz.github.io/gallery/step_chart.html. However: in the 538 viz the value for the final date must be extended for the full width of that last element. In the step chart example and my solution, the line stops as soon as the last date element is met.

I have looked at altair's github and google groups and found nothing similar to this problem.

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')
)

extra=base.transform_calculate(cleanpass='datum.clean_test == "ok" ? "PASS" : datum.clean_test == "dubious" ? "PASS" : "FAIL"'
                      ).mark_line(interpolate='step-after'
                      ).encode(alt.Color("cleanpass:N",scale=alt.Scale(domain=['PASS','FAIL'],range=['black','white']))
                      )



alt.layer(main,extra).configure_scale(
    bandPaddingInner=0.01 #smaller vertical lines
).resolve_scale(color='independent')

来源:https://stackoverflow.com/questions/57878892/drawing-line-plot-for-a-histogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!