從 SQLAlchemy 結果到圖表
我們也可以善用 pandas 和 Matplotlib 來將資料繪製成圖。記住,資料視覺化對探索性資料分析與溝通你的資料都至關重要!
本練習屬於課程
Python 資料庫入門
練習說明
- 匯入
matplotlib.pyplot並命名為plt。 - 使用提供的
results呼叫pd.DataFrame()建立 DataFramedf。 - 將 DataFrame 的欄位
df.columns設為第一個結果物件的欄位results[0].keys()。 - 列印 DataFrame
df。 - 在
df上使用plot.bar()建立長條圖。 - 使用
plt.show()顯示圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import pyplot as plt from matplotlib
# Create a DataFrame from the results: df
df = ____
# Set Column names
df.columns = ____
# Print the DataFrame
# Plot the DataFrame