美文网首页Python
用Tableau画彩虹背景散点图

用Tableau画彩虹背景散点图

作者: 扫地sir | 来源:发表于2019-10-22 11:47 被阅读0次

    以前就看到过不少带背景的散点图,但是有些下载下来一看,就是一张做好的背景图片,最近看到一个Viz作品《Scatter Plot Reference Bands》(意思就是“有参考带的散点图”),就完全是利用计算字段画出来的背景带,所以就兴奋地开始研究。下面就给大家分享一下制作过程。

    https://public.tableau.com/profile/rajvivan#!/vizhome/WorkoutWednesday36-ScatterPlotReferenceBand/WorkoutWednesday36-ScatterPlotReferenceBand

    准备数据集

    Player,Adjust Games,Games,PTS,Is Active
    Adrian Dantley,955,1230,23177,Inactive Players
    Alex English,1193,1230,25613,Inactive Players
    Allen Iverson,914,1116,24368,Inactive Players
    Antawn Jamison,1083,1264,20042,Inactive Players
    Bernard King,874,1312,19655,Inactive Players
    Bob Lanier,958,1148,19246,Inactive Players
    Bob Pettit,792,836,20880,Inactive Players
    Carmelo Anthony,1054,1151,25417,Active Players
    Charles Barkley,1073,1280,23757,Inactive Players
    Clifford Robinson,1380,1444,19591,Inactive Players
    Clyde Drexler,1086,1230,22195,Inactive Players
    David Robinson,987,1116,20790,Inactive Players
    Dirk Nowitzki,1471,1624,31187,Active Players
    Dominique Wilkins,1074,1362,26666,Inactive Players
    Dwyane Wade,982,1230,22082,Active Players
    Elgin Baylor,846,1117,23133,Inactive Players
    Elvin Hayes,1303,1312,27311,Inactive Players
    Gary Payton,1335,1362,21813,Inactive Players
    George Gervin,791,820,20708,Inactive Players
    Hakeem Olajuwon,1238,1444,26946,Inactive Players
    Hal Greer,1122,1199,21590,Inactive Players
    Jerry West,932,1134,25197,Inactive Players
    Joe Johnson,1276,1378,20405,Active Players
    John Havlicek,1270,1301,26395,Inactive Players
    John Stockton,1504,1526,19711,Inactive Players
    Kareem Abdul-Jabbar,1560,1560,38392,Inactive Players
    Karl Malone,1476,1476,36928,Inactive Players
    Kevin Durant,771,886,20913,Active Players
    Kevin Garnett,1462,1559,26071,Inactive Players
    Kobe Bryant,1346,1564,33643,Inactive Players
    Larry Bird,897,1066,21791,Inactive Players
    LeBron James,1143,1214,31038,Active Players
    Michael Jordan,1072,1455,32292,Inactive Players
    Mitch Richmond,976,1116,20497,Inactive Players
    Moses Malone,1329,1558,27405,Inactive Players
    Oscar Robertson,1040,1134,26712,Inactive Players
    Patrick Ewing,1183,1330,24815,Inactive Players
    Pau Gasol,1196,1378,20776,Active Players
    Paul Pierce,1343,1510,26397,Inactive Players
    Ray Allen,1300,1416,24505,Inactive Players
    Reggie Miller,1389,1444,25279,Inactive Players
    Robert Parish,1611,1722,23335,Inactive Players
    Shaquille ONeal,1207,1526,28596,Inactive Players
    Terry Cummings,1183,1444,19462,Inactive Players
    Tim Duncan,1392,1510,26496,Inactive Players
    Tom Chambers,1107,1312,20049,Inactive Players
    Vince Carter,1405,1519,24868,Active Players
    Walt Bellamy,1043,1143,20943,Inactive Players
    Walter Davis,1033,1230,19521,Inactive Players
    Wilt Chamberlain,1045,1127,31417,Inactive Players
    

    数据集很简单,包括NBA球员名称,调整后的出场次数,出场次数(没用上这个字段),总得分,是否退役

    下面要用Tableau的并集功能,把数据集复制4次。

    用Tableau打开数据集,我这里命名为Data,并拖动到数据源里,然后点击右侧向下的箭头,选中编辑并集,然后再拖动3次Data到编辑面板,这时会出现4个Data数据集,最后点击一下“应用”按钮,就会出现下图的Data,Data1,Data2,Data3。

    这时,数据集已经从原有的50行扩充到200行,并多出了Table Name字段,来区分Data-Data3。

    这里就要说一下,为什么要复制4份数据?因为这里画的背景带其实是个三角形。画一个封闭的三角形需要4个点,点1和4在原点重合,2和3在最右侧,请参考下图:

    创建计算字段

    • 创建参数Band Size,以方便调整每组的大小
    • Avg Points per Game=INT ({FIXED [Player]: AVG ([PTS] / [Adjust Games])})

    • Low Band=INT ([Avg Points per Game] / [Band Size])* [Band Size]

    取每组的下线,加INT是取整,保证每组的范围都是整数

    • Band=STR([Low Band])+"-"+STR([Low Band]+[Band Size]-1)

    这个字段就确定了各组的取值范围,比如用默认参数5,Band就是10-14,15-19,20-24,25-29,30-34这几个组

    • Dot=
    CASE [Table Name]
    WHEN "Data" THEN 1
    WHEN "Data1" THEN 2
    WHEN "Data2" THEN 3
    WHEN "Data3" THEN 4
    END
    

    这个字段就用到了我们上面做的并集,把并集改成可以1-4,以便确定画三角形时候的路径

    • X=
    CASE [Dot]
    WHEN 1 THEN 0
    WHEN 2 THEN 1700        
    WHEN 3 THEN 1700
    WHEN 4 THEN 0
    END
    

    这里用1700,是因为Adjust Games的最大值是1611,实际应用中也可以写计算字段算一下最大值,或者用参数,这里就偷懒了。

    • Y=
    [X]*
    CASE [Dot]
    WHEN 1 THEN [Low Band]
    WHEN 2 THEN [Low Band]
    WHEN 3 THEN [Low Band]+[Band Size]-0.1
    WHEN 4 THEN [Low Band]+[Band Size]-0.1
    END
    

    这里减去0.1是为了各背景带之间有个空白间隔,其实不减也没问题,用颜色里的边界也有同样的效果,大家可以自己尝试。

    开始画图

    • 拖动[X]到列功能区,[Y]到行功能区,右键选择度量为最小值(用右键拖动也可以直接选择度量计算依据)

    这要强调一定要用最小值,原因大家自行研究。

    • 标记类型选择“多边形”,拖动[Dot]到“路径”
    • 拖动[Band]到颜色,默认参数如果是5,就会出现下图
    • 拖动[PTS]到行功能区,[Adjust Games]到列功能区,右键选择度量为平均值

    • 在标记功能区“平均值(PTS)”里,选择标记类型为“形状”,拖动[Is Active]到形状,并选好合适形状

    • 拖动[Player]到“详细信息”,并把“详细信息”里的[Dot]去掉,就得到下图

    • 在行、列里都选择双轴,并同步轴

    • 这时颜色标签会多一个“度量名称”,会把颜色分类变多,这是我们不需要的,去掉这个“度量名称”的颜色,然后调整背景带的颜色透明度

    这里左侧的X=0这列的圆点是没有意义的,但是我也没有找到方法去掉,原Viz作品也是保留的,只是通过固定轴,不显示这些圆点

    • 最后调整固定X、Y轴的范围,就得到了想要的效果

    今天的分享就到这里,这里只是简单介绍一下方法,大家可以下载原Viz作品继续深入研究。

    此篇文章已发布到我的公众号:saodisir,有兴趣也可关注一下

    相关文章

      网友评论

        本文标题:用Tableau画彩虹背景散点图

        本文链接:https://www.haomeiwen.com/subject/wcgfvctx.html