6 สัปดาห์ที่แล้วฉันลาออกจากงานที่ปรึกษาด้านการจัดการและเริ่มหลักสูตรติวเข้มด้านวิทยาศาสตร์ข้อมูล โลกของฉันเปลี่ยนจากสไลด์สวย ๆ และการประชุม MS Teams แบบกลับไปกลับมาไปจนถึงดาวเคราะห์สัตว์และภาษาที่ตายแล้ว (อ่าน: สมุดบันทึก Jupyter, Python, แพนด้าและสถิติในสัญกรณ์คณิตศาสตร์)
สิ่งหนึ่งที่ฉันจะนำไปข้างหน้าเสมอจากการให้คำปรึกษาคือความสำคัญของการเล่าเรื่อง ใครจะสนใจว่าฉันจะสร้างโมเดลได้กี่แบบหากไม่มีใครเข้าใจวิธีการใช้งานและเหตุใดจึงสำคัญ และตามที่สุภาษิตกล่าวไว้ว่ารูปภาพมีค่าหนึ่งพันคำ
เป็นไปไม่ได้ที่จะพูดเกินจริงถึงความสำคัญของภาพที่เรียบง่ายชัดเจนในสาขาวิทยาศาสตร์ข้อมูลและการวิเคราะห์
ใครก็ตามที่ขลุกอยู่กับวิทยาศาสตร์ข้อมูลน่าจะได้รับการแนะนำ Matplotlib ซึ่งเป็นไลบรารีการวางแผนฟรีมาตรฐานที่ได้รับแรงบันดาลใจจาก MATLAB ที่เข้มข้นขึ้น / เข้าถึงได้น้อยกว่า ใช้งานง่ายและใช้งานง่ายเหมาะสำหรับผู้เริ่มต้น
ปัญหาคือ… มันน่าเกลียดและการปรับแต่งความงามอาจใช้รหัสจำนวนมากเกินไป
Plotly เป็นไลบรารีกราฟโอเพ่นซอร์สฟรีที่สามารถใช้กับภาษาการเข้ารหัสหลายภาษารวมถึง Python, Javascript และ R ในขณะที่โครงสร้างและความสามารถในการวางแผนและ matplotlib มีความคล้ายคลึงกันประโยชน์ที่ชัดเจนสามประการของการวางแผนมากกว่า matplotlib คือ:
มาสาธิตตัวอย่างง่ายๆ
ขั้นแรกให้นำเข้าแพ็คเกจมาตรฐานที่เราจะใช้ตลอดทั้งตัวอย่าง
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
เราจะเริ่มต้นด้วยการกระจายแผนภาพที่เรียบง่ายเพื่อแสดงให้เห็นถึงสุนทรียภาพพื้นฐานของแต่ละห้องสมุด
สร้างข้อมูลตัวอย่างสำหรับเส้นโค้ง 2 มิติและลงจุดโดยใช้ matplotlib:
xaxis = np.linspace(0, 10, 100)
yaxis = np.sin(xaxis)
mat_scatter = plt.scatter(x=xaxis, y=yaxis)
plt.title('Basic Matplotlib Scatter')
plt.show();
plotly_scatter = px.scatter(x=xaxis, y=yaxis, title='Basic Plotly Scatter')
plotly_scatter.show()
ฮิสโตแกรมเป็นวิธีที่ยอดเยี่ยมในการแสดงการโต้ตอบนอกกรอบของพล็อตเรื่อง
สร้างดาต้าเฟรมโดยใช้ชุดข้อมูลของแพนด้าตัวใดตัวหนึ่งจากนั้นแบ่งดาต้าเฟรม 3 แบบที่แตกต่างกันตามคอลัมน์ 'สายพันธุ์' ที่เป็นหมวดหมู่ นี่เป็นการตั้งค่าให้เราแสดงให้เห็นว่าสามารถสร้างพล็อตหลาย ๆ แบบในรูปเดียวได้อย่างไร
df = px.data.iris()
setosa_df = df[df['species'] == 'setosa']
versicolor_df = df[df['species'] == 'versicolor']
virginica_df = df[df['species'] == 'virginica']
mat_hist = plt.hist(x=setosa_df['petal_length'])
plt.title('Matplotlib Histogram - Setosa Petal Lengths')
plt.xlabel('Petal Length')
plt.ylabel('Count')
plt.savefig('images/mat_hist.png')
plt.show();
PNG file written from code above
plotly_hist = px.histogram(setosa_df, x='petal_length', title='Plotly Histogram - Setosa Petal Lengths', labels={'petal_length': 'Petal Length'})
plotly_hist.write_html('images/plotlyhistogram.html')
plotly_hist.write_image('images/plotly_hist.png')
plotly_hist.show()
PNG file written from plotly code above
Plotly graphs by default are interactive, meaning you can hover over the graph to see details of the underlying data, zoom in/out, etc. without adding any code whatsoever. In our setosa petal lengths histogram example, users can hover over each bin to see the range of x-values captured in that bin (in this case, range of petal lengths), and the count of elements that fall into that bin. The x-axis was automatically labelled using the column header from the dataframe. Passing in a dictionary of original value: desired label to the “labels” parameter, I can easily make those labels more intuitive for a user who may not be as familiar with the underlying dataset. You can save figures as static PNG/JPG files or as interactive HTML files that open in a web browser. The interactive version of the figure renders automatically in Jupyter Notebook.
ทั้ง matplotlib และ plotly สามารถพล็อตพล็อตย่อยหลาย ๆ แบบลงในรูปเดียวเพื่อแสดงการเปรียบเทียบ เราจะสาธิตการใช้ข้อมูลเดียวกันกับไอริส
ใช้ matplotlib สร้างรูปที่มี 3 คอลัมน์จากนั้นพล็อตฮิสโทแกรมของความยาวกลีบดอกไม้แต่ละชนิดลงในรูป
mat_hist_fig, axes = plt.subplots(ncols=3, figsize=(15,5), sharex=True, sharey=True)
mat_hist_fig.set_tight_layout(tight=True)
mat_hist_fig.suptitle('Matplotlib Histograms - Iris Petal Lengths by Species')
axes[0].hist(setosa_df['petal_length'], bins=5, color='navy')
axes[0].set_title('Setosa')
axes[0].set(ylabel='Count')
axes[0].set(xlabel='Petal Length')
axes[1].hist(versicolor_df['petal_length'], bins=5, color='purple')
axes[1].set_title('Versicolor')
axes[1].set(xlabel='Petal Length')
axes[2].hist(virginica_df['petal_length'], bins=5, color='cornflowerblue')
axes[2].set_title('Virginica')
axes[2].set(xlabel='Petal Length')
mat_hist_fig.savefig('images/mat_hist_fig.png');
PNG file written by code above
plotly_hist_fig = make_subplots(rows=1, cols=3, subplot_titles=('Setosa', 'Versicolor', 'Virginica'), shared_yaxes=True, shared_xaxes=True, horizontal_spacing=.02)
plotly_hist_fig.update_layout(title_text='Plotly Histograms - Iris Petal Lengths by Species', showlegend=False, height=400)
plotly_hist_fig.update_xaxes(title_text='Petal Length', range=[0,8])
plotly_hist_fig.update_yaxes(title_text='Count', col=1)
plotly_hist_fig.add_trace(
go.Histogram(x=setosa_df['petal_length'], name='Setosa', marker_color='navy', nbinsx=5),
1, 1)
plotly_hist_fig.add_trace(
go.Histogram(x=versicolor_df['petal_length'], name='Versicolor', marker_color='purple', nbinsx=5),
1, 2)
plotly_hist_fig.add_trace(
go.Histogram(x=virginica_df['petal_length'], name='Virginica', marker_color='cornflowerblue', nbinsx=5),
1, 3)
plotly_hist_fig.write_image('images/plotly_hist_fig.png')
plotly_hist_fig.show()
PNG file written by plotly code above
สิ่งที่ยอดเยี่ยมที่สุดที่ฉันค้นพบโดยใช้พล็อตเรื่องคือการปรับแต่งแอนิเมชันแบบหลอกๆ
ใช้ข้อมูลเดียวกันและสถานการณ์เดียวกันกับรูปด้านบนสร้างฮิสโตแกรมแบบเคลื่อนไหวในพล็อตโดยตั้งค่าพารามิเตอร์ "animation_frame" เป็นองค์ประกอบที่เป็นหมวดหมู่ (ในกรณีของเราคือสายพันธุ์)
plotly_hist_anim = px.histogram(df, x='petal_length', nbins=5, title='Plotly Histogram - Iris Petal Lengths', labels={'petal_length': 'Petal Length'}, animation_frame='species', color='species', color_discrete_sequence=['navy', 'purple', 'cornflowerblue'])
plotly_hist_anim.update_layout(xaxis_range=(0, df['petal_length'].max()), yaxis_range=(0, 25))
plotly_hist_anim.write_html('images/plotly_hist_anim.html')
plotly_hist_anim.show()
ดังที่คุณเห็นจากตัวอย่างง่ายๆไม่กี่ตัวอย่างการวางแผนที่เหนือกว่าไลบรารีกราฟของ matplotlib ในด้านความเรียบง่ายความสง่างามการโต้ตอบและภาพเคลื่อนไหวดังนั้นแม้แต่ ETL ของคุณก็สวยงาม
ถัดไปเพื่อสำรวจ: Dashของ plotly ซึ่งสามารถสร้างแอปการสร้างภาพข้อมูลบนเว็บแบบโต้ตอบด้วยรหัส Python ที่บริสุทธิ์
Badreesh Shetty เขียนบล็อกที่มีรายละเอียดเกี่ยวกับไลบรารี Matplotlib
เอกสารของ Plotlyนั้นดีและใช้งานง่ายแม้ว่าฉันจะเจอบางสิ่งที่ล้าสมัยดังนั้นเตรียมตัวให้พร้อมสำหรับการลองผิดลองถูก พวกเขาแสดงตัวอย่างและประเภทกราฟขั้นสูงมากมาย
คุณต้องเคยได้ยินเกี่ยวกับปลั๊กอิน ChatGPT อันทรงพลังใหม่ทั้งหมด เช่น เบราว์เซอร์ ตัวแปลรหัส ฯลฯ แต่มีปัญหา การเข้าถึงปลั๊กอินถูกจำกัดไว้เฉพาะรายการรอ และหลายคนไม่สามารถเข้าถึงได้
สัปดาห์นี้ ฉันได้รับอีเมลจากสมาชิกรายการอีเมลรายวัน พวกเขาต้องการทราบ: คุณเข้าชมแหล่งข้อมูลใดรายวัน รายสัปดาห์ และรายเดือน คำถามที่ดี! ฉันยินดีที่จะพยายามแบ่งปันข้อมูลเชิงลึกเกี่ยวกับเวิร์กโฟลว์และทรัพยากรยอดนิยมที่ฉันใช้
เมื่อคุณส่งคำขอไปที่ facebook.com คอมพิวเตอร์ของคุณจะค้นหาชื่อโดเมนเพื่อแปลชื่อโดเมนนั้นเป็นที่อยู่ IP เพื่อให้คอมพิวเตอร์ของคุณสามารถเชื่อมต่อได้
“หวัดดี Siri เล่น Me Dive โดย Olivia Dean” ในเดือนพฤษภาคม 2021 ฉันเริ่มฝึกงานที่ Future Academy Africa ในเมือง Akure ประเทศไนจีเรีย
หากเราต้องการจะเติบโตบนโลกใบนี้ต่อไป เราจะต้องสนทนาที่อันตราย คนที่ท้าทายสิ่งที่เราทำ สิ่งที่เรากำลังทำ และสิ่งที่เรากำลังจะทำต่อไป (หรือกำลังวางแผนที่จะทำ)
เมื่อวานนี้ เราปิดการแข่งขัน Eurovision Song Contest ปีนี้ด้วยตอนจบที่สดใสและวุ่นวาย โดย Loreen นักร้องชาวสวีเดนคว้าถ้วยรางวัลกลับบ้าน กลายเป็นผู้หญิงคนแรกที่ชนะสองครั้งและนำมงกุฎกลับมายังสวีเดน 50 ปีหลังจาก ABBA ชนะการแข่งขัน กับ “วอเตอร์ลู” พวกเขากลายเป็นหนึ่งในการแสดงที่ยิ่งใหญ่ที่สุดในวงการเพลงป๊อป