背景
一圖勝千言,優秀的可視化圖表不僅能以直觀、簡潔的方式呈現復雜的信息,還能夠通過圖形、顏色和布局的巧妙設計,引發觀眾的情感共鳴,增強數據背后故事的表達力與說服力。它們超越了單純的數據展示,更能幫助觀眾深入理解潛在的趨勢、關系和模式,從而更有效地支持決策和行動
小編環境
import sys
print('python 版本:',sys.version)
#python 版本: 3.11.11 | packaged by Anaconda, Inc. |
#(main, Dec 11 2024, 16:34:19) [MSC v.1929 64 bit (AMD64)]
import matplotlib
print(matplotlib.__version__)
#3.10.0
import numpy
print(numpy.__version__)
#2.2.2
效果展示
plasma配色
viridis配色
完整代碼
#導入庫
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
#生成數據
num_points = 100
x,y = np.random.rand(2,num_points)*10
#生成顏色和大小數據
colors = np.random.rand(num_points)
sizes =np.random.rand(num_points)*1000
#創建圖形和散點圖
fig, ax = plt.subplots()
scat =ax.scatter(x,y,c=colors,s=sizes, alpha=0.7, cmap='plasma') #viridis
ax.set_xlim(0,10)
ax.set_ylim(0,10)
#創建動畫更新函數
def animate(i):
new_x=x+np.random.randn(num_points)*0.1
new_y=y+np.random.randn(num_points)*0.1
scat.set_offsets(np.c_[new_x,new_y]) #更新散點圖中所有點的坐標
return scat,
ani = animation.FuncAnimation(fig,animate,frames=100,interval=150)
ani.save('plasma.gif', writer='pillow', fps=10) #viridis
這里重點是創建動畫更新函數 animate
,用于更新每一幀散點圖中的點的位置,傳入的參數 i
代表幀編號參數,是 FuncAnimation
每次調用更新函數時傳入的參數,表示當前動畫幀的索引
歷史相關文章
以上是自己實踐中遇到的一些問題,分享出來供大家參考學習,歡迎關注微信公眾號:DataShare ,不定期分享干貨