การแสดงข้อมูลด้วย Plotly Part-II

Feb 07 2023
สรุป ก่อนหน้านี้ เราได้ทำงานเกี่ยวกับการสร้างแผนภูมิแท่งเพื่อให้เห็นภาพจำนวนประชากรรุ่นปีเกิดที่มีการจัดอันดับตามจำนวนคนที่มีชีวิตอยู่มากที่สุด ข้อมูลนี้ได้มาจากการสำรวจสำมะโนประชากรของสหรัฐอเมริกาในปี 2019 ซึ่งระบุอายุและจำนวนประชากรทั้งหมดของสหรัฐอเมริกาในช่วงอายุนั้น

สรุป

ก่อนหน้านี้เราทำงานเกี่ยวกับการสร้างแผนภูมิแท่งเพื่อให้เห็นภาพจำนวนประชากรรุ่นปีเกิดที่มีการจัดอันดับตามจำนวนคนที่มีชีวิตอยู่มากที่สุด ข้อมูลนี้ได้มาจากการสำรวจสำมะโนประชากรของสหรัฐอเมริกาในปี 2019 ซึ่งระบุอายุและจำนวนประชากรทั้งหมดของสหรัฐอเมริกาในช่วงอายุนั้น เราใช้ห้องสมุด Pandas เพื่ออ่านและประมวลผลข้อมูล

เราใช้วิธี update_traces ของ Plotly เพื่ออัปเดตรูปลักษณ์ของแผนภูมิ รวมถึงสีพื้นหลัง เครื่องหมาย และชื่อแกน นอกจากนี้เรายังใช้วิธี update_layout ของ Plotly เพื่อปรับชื่อแผนภูมิในทิศทาง x ผลลัพธ์สุดท้ายคือแผนภูมิแท่งที่ดึงดูดสายตาซึ่งถ่ายทอดข้อมูลในข้อมูลได้อย่างมีประสิทธิภาพ

ได้เวลายกระดับแผนภูมิ Plotly ของเราไปอีกขั้นแล้ว! มาเพิ่มลูกเล่นและสไตล์เพื่อทำให้เป็นแผนภูมิที่มีภาพสวยงามที่สุดในเมืองกันเถอะ ได้เวลาเปิดพิซซ่าแล้ว!

เอาล่ะทุกคน ได้เวลาเจาะจงแล้ว! มาดูกันว่าคนรุ่นมิลเลนเนียล (1981–1997) เทียบชั้นกับรุ่นอื่นๆ ได้อย่างไร ถึงเวลาเพิ่มความแม่นยำให้กับแผนภูมิแท่งนี้และให้เครดิตแก่คนรุ่นมิลเลนเนียล (หรือตำหนิ ขึ้นอยู่กับมุมมองของคุณ)

อันดับแรก เราจะใช้add_vlineวิธีการเพิ่มเส้นแนวตั้ง และadd_annotationวิธีการติดป้ายกำกับแต่ละบรรทัดด้วยปี มันเหมือนกับการเพ่งความสนใจไปที่รัชสมัยของคนรุ่นต่างๆ

# Step 6 - Specifying Generation Reigns
datetoannote = [1927.5, 1945.5, 1964.5, 1980.5, 1996.5, 2012.5]

for i in datetoannote:
    fig.add_vline(
            x=i,
            fillcolor="black", opacity=1,layer="above", line_width=2,
            line_dash="dot"
        )

    fig.add_annotation(
            text=str(int(i+0.5)),
            xref="x", yref="y",textangle=270,
            x=i+1, y=4800000, showarrow=False
        )

      
                


# step 7 adding annotation
y = 6600000
y_toadd = 200000
fig.add_annotation(
            text="#7<br><b>GREATEST <br> GENERATION</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=1920, y=y, showarrow=False
        )
fig.add_annotation(
            text="#6<br><b>SILENT <br> GENERATION</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=1937, y=y, showarrow=False
        )
fig.add_annotation(
            text="#2<br><b>BOOMERS</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=1955, y=y+y_toadd, showarrow=False
        )
fig.add_annotation(
            text="#4<br><b>GENERATION X</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color = 'black'),
            align = 'center', x=1972.5, y=y+y_toadd, showarrow=False
        )
fig.add_annotation(
            text="#1<br><b>MILLENNIALS</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=1988.5, y=y+y_toadd, showarrow=False
        )
fig.add_annotation(
            text="#3<br><b>GENERATION Z</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=2004.5, y=y+y_toadd, showarrow=False
        )
fig.add_annotation(
            text="#5<br><b>GENERATION <br> ALPHA</b><br>",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=14,color='black'),
            align = 'center', x=2020, y=y, showarrow=False
        )


fig.add_annotation(
            text="1.7M  |  0.5%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=1920, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="23.2M  |  7.1%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=1937, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="71.6M  |  21.8%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=1955, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="65.2M  |  19.9%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=1973, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="72.1M  |  22.0%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=1989, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="66.9M  |  20.4%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=2005, y=5550000, showarrow=False
        )
fig.add_annotation(
            text="27.6M  |  8.4%",
            xref="x", yref="y",textangle=0,
            font=dict(family="Trebuchet MS",size=12,color='black'),
            x=2020, y=5550000, showarrow=False
        )
fig.add_annotation(
            text=" Total  %POP.",
            font=dict(family="Courier New, monospace",size=11,color = 'black'),
            xref="x", yref="y",textangle=0,align="center",
            x=1920, y=5200000, showarrow=False
        )
fig.add_annotation(
            text="Those born before<br> 1920 were grouped<br> together",
            font=dict(family="Courier New, monospace",size=10,color = 'black'),
            xref="x", yref="y",textangle=0,align="right",
            x=1919, y=100322, showarrow=True
        )

      
                


# Step 8 - update axes
fig.update_xaxes(
            range=[1915,2020],
            
            showline=True,
            linecolor='black',
            linewidth=3,
            
            showgrid=False,
    
            layer='above traces',
            
            showticklabels=True,
            tickmode = 'array',
            tickvals = [1920, 1930, 1940, 1950, 1960, 1970,
                        1980, 1990, 2000, 2010, 2019],
            ticks="outside",
            tickwidth=1.5,
            tickcolor='black',
            ticklen=10,
            tickprefix = "  ",
            ticksuffix = "  ",
    
            fixedrange=True,
        )

fig.update_yaxes(
            showline = False,
            
            showgrid=False,
            
            fixedrange=True,
            
            zeroline = False,
    
            tickmode = 'array',
            ticktext=[0, "1 ", "2 ", "3 ", "4 "],
            tickvals = [0, 1000000, 2000000, 3000000, 4000000],
            ticks="inside",
            tickwidth=1,
            tickcolor='black',
            ticklen=10,
        )

      
                

#updating fig size, margin and titles
fig.update_layout(
            height=700,
            margin=dict(t=120, l=60, r=60, b=60, pad = 0),
            title="",
            yaxis_title="",
            xaxis_title=""
        )

fig.add_annotation(
              text="BIRTH YEAR",
              xref="paper", yref="paper",
              x=0.01, y=-0.09, showarrow=False
        )
fig.add_annotation(
              text="MILLION PEOPLE",
              xref="paper", yref="paper",
              x=-0.02, y=0.65, showarrow=False
        )
fig.add_annotation(
              text="Compiled from population estimates from the\
                    United States Census Bureau for July 2019",
              xref="paper", yref="paper",
              font=dict(family="Courier",size=11,color = 'black'),
              x=1.05, y=-0.12, showarrow=False
        )
fig.add_shape(
              type="rect",x0=0.02, y0=1.09, x1=0.03, y1=1.15,
              xref='paper', yref='paper',
              line=dict(color='black',width=1),
              fillcolor='black',opacity = 0.5
        )
fig.add_annotation(
            text="GENERATION POPULATIONS,",
            xref='paper', yref='paper',x= 0.03,y=1.14,
            textangle=0,align="right",showarrow=False,
            font=dict(size=25, family="Trebuchet MS", color="#00AD90")
        )
fig.add_annotation(
            text="WITH RANKINGS BY MOST NUMBER OF PEOPLE ALIVE",
            xref='paper', yref='paper', x=0.61, y=1.125,
            textangle=0,align="right",showarrow=False,
            font=dict(size=15, family="Trebuchet MS", color="#00C3A9")
        )


      
                

แล้วทำไมต้องหยุดอยู่แค่นั้น? เราสามารถเล่นกับสีต่อไปได้จนกว่าแผนภูมิจะดูเหมือนสีรุ้งหรือยูนิคอร์นโผล่ขึ้นมา! ความเป็นไปได้ไม่มีที่สิ้นสุด

เอาล่ะทุกคนนั่นคือการสรุป! ฉันหวังว่าคุณจะสนุกกับการแสดงและได้เรียนรู้สิ่งใหม่ๆ หากคุณพบว่าสิ่งนี้มีประโยชน์ โปรดยกนิ้วให้และแจ้งให้เราทราบว่าคุณคิดอย่างไรในความคิดเห็น จนกว่าจะถึงครั้งต่อไป ขอให้มีความสุขในการวางแผน