自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

結(jié)合 NumPy 和 Matplotlib 進(jìn)行數(shù)據(jù)可視化的十種創(chuàng)意

開發(fā) 后端 數(shù)據(jù)可視化
本文我們可以看到 NumPy 和 Matplotlib 在數(shù)據(jù)可視化中的強(qiáng)大能力,無論是簡(jiǎn)單的正弦波形還是復(fù)雜的等高線圖,都能輕松實(shí)現(xiàn)。

大家好!今天我們要聊的是如何使用 NumPy 和 Matplotlib 來進(jìn)行數(shù)據(jù)可視化。這兩個(gè)庫是 Python 中處理數(shù)值數(shù)據(jù)和繪圖的強(qiáng)大工具。NumPy 讓我們可以高效地處理數(shù)組數(shù)據(jù),而 Matplotlib 則提供了豐富的圖表繪制功能。

1. 基礎(chǔ)數(shù)據(jù)類型可視化

首先,讓我們從最基礎(chǔ)的數(shù)據(jù)類型開始。NumPy 可以創(chuàng)建各種類型的數(shù)組。Matplotlib 可以將這些數(shù)組轉(zhuǎn)化為直觀的圖表。

import numpy as np
import matplotlib.pyplot as plt

# 創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)組
x = np.linspace(0, 10, 100)
y = np.sin(x)

# 使用 Matplotlib 繪制圖形
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()

這段代碼生成了一個(gè)簡(jiǎn)單的正弦波形圖。np.linspace 函數(shù)用于生成等差數(shù)列,np.sin 用于計(jì)算正弦值。plt.plot 函數(shù)繪制曲線,plt.title, plt.xlabel, plt.ylabel 設(shè)置圖表標(biāo)題和軸標(biāo)簽。

2. 多重?cái)?shù)據(jù)系列可視化

接下來,讓我們嘗試同時(shí)繪制多個(gè)數(shù)據(jù)系列。這在比較不同數(shù)據(jù)集時(shí)非常有用。

# 創(chuàng)建兩個(gè)不同的數(shù)據(jù)系列
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 繪制兩個(gè)數(shù)據(jù)系列
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.legend()  # 顯示圖例
plt.show()

這里,我們?cè)黾恿?nbsp;plt.legend() 函數(shù),它會(huì)根據(jù) label 參數(shù)自動(dòng)添加圖例。這樣就可以區(qū)分不同的數(shù)據(jù)系列了。

3. 散點(diǎn)圖可視化

散點(diǎn)圖非常適合顯示離散數(shù)據(jù)之間的關(guān)系。例如,我們可以用它來表示兩個(gè)變量之間的相關(guān)性。

# 創(chuàng)建隨機(jī)數(shù)據(jù)
x = np.random.randn(100)
y = np.random.randn(100)

# 繪制散點(diǎn)圖
plt.scatter(x, y)
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

np.random.randn 生成標(biāo)準(zhǔn)正態(tài)分布的隨機(jī)數(shù)。plt.scatter 用于繪制散點(diǎn)圖。

4. 直方圖可視化

直方圖可以用來展示數(shù)據(jù)的分布情況。這對(duì)于分析數(shù)據(jù)頻率非常有幫助。

# 創(chuàng)建隨機(jī)數(shù)據(jù)
data = np.random.randn(1000)

# 繪制直方圖
plt.hist(data, bins=30, alpha=0.7)
plt.title('Histogram')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

plt.hist 用于繪制直方圖,bins 參數(shù)指定直方圖的柱子數(shù)量,alpha 參數(shù)設(shè)置透明度。

5. 等高線圖可視化

等高線圖適用于展示二維函數(shù)的等值線。這在地理信息系統(tǒng)中很常見。

# 創(chuàng)建網(wǎng)格數(shù)據(jù)
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sqrt(X**2 + Y**2)

# 繪制等高線圖
plt.contourf(X, Y, Z, 20, cmap='viridis')
plt.colorbar()
plt.title('Contour Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

np.meshgrid 用于創(chuàng)建網(wǎng)格數(shù)據(jù),plt.contourf 繪制等高線圖,cmap 參數(shù)設(shè)置顏色映射。

6. 熱力圖可視化

熱力圖常用于展示二維數(shù)據(jù)矩陣,非常適合展示數(shù)據(jù)的相關(guān)性或密度。

# 創(chuàng)建一個(gè)隨機(jī)的二維數(shù)據(jù)矩陣
data = np.random.rand(10, 10)

# 繪制熱力圖
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.title('Heatmap')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

plt.imshow 用于繪制熱力圖,cmap 參數(shù)設(shè)置顏色映射,interpolation 參數(shù)設(shè)置插值方法。

7. 餅圖可視化

餅圖用于展示各個(gè)部分占總體的比例,非常適合展示分類數(shù)據(jù)。

# 創(chuàng)建分類數(shù)據(jù)
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]

# 繪制餅圖
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title('Pie Chart')
plt.show()

plt.pie 用于繪制餅圖,autopct 參數(shù)用于顯示百分比,startangle 參數(shù)設(shè)置起始角度。

8. 箱線圖可視化

箱線圖用于展示數(shù)據(jù)的分布情況,特別是四分位數(shù)和異常值。

# 創(chuàng)建隨機(jī)數(shù)據(jù)
data = np.random.randn(100)

# 繪制箱線圖
plt.boxplot(data)
plt.title('Box Plot')
plt.ylabel('Value')
plt.show()

plt.boxplot 用于繪制箱線圖,它可以清晰地展示數(shù)據(jù)的中位數(shù)、四分位數(shù)和異常值。

9. 三維可視化

Matplotlib 還支持三維可視化,這對(duì)于展示多維數(shù)據(jù)非常有用。

from mpl_toolkits.mplot3d import Axes3D

# 創(chuàng)建三維數(shù)據(jù)
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# 創(chuàng)建三維圖形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')

ax.set_title('3D Surface Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
plt.show()

mpl_toolkits.mplot3d 模塊提供了三維繪圖功能,plot_surface 用于繪制三維表面圖。

10. 動(dòng)態(tài)可視化

動(dòng)態(tài)可視化可以展示數(shù)據(jù)隨時(shí)間的變化,非常適合展示時(shí)間序列數(shù)據(jù)。

import matplotlib.animation as animation

# 創(chuàng)建數(shù)據(jù)
x = np.linspace(0, 10, 100)
y = np.sin(x)

# 創(chuàng)建圖形對(duì)象
fig, ax = plt.subplots()
line, = ax.plot(x, y)

# 更新函數(shù)
def update(frame):
    line.set_ydata(np.sin(x + frame / 10.0))
    return line,

# 創(chuàng)建動(dòng)畫
ani = animation.FuncAnimation(fig, update, frames=100, interval=50)
plt.title('Dynamic Sine Wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()

matplotlib.animation 模塊提供了動(dòng)畫功能,F(xiàn)uncAnimation 用于創(chuàng)建動(dòng)畫,update 函數(shù)定義每一幀的更新邏輯。

實(shí)戰(zhàn)案例:股票價(jià)格走勢(shì)分析

假設(shè)我們有一個(gè)包含某股票每日收盤價(jià)的數(shù)據(jù)集,我們想要分析其價(jià)格走勢(shì)并預(yù)測(cè)未來趨勢(shì)。

import pandas as pd
import yfinance as yf

# 下載股票數(shù)據(jù)
ticker = 'AAPL'
data = yf.download(ticker, start='2022-01-01', end='2023-01-01')

# 繪制股票價(jià)格走勢(shì)圖
plt.figure(figsize=(12, 6))
plt.plot(data['Close'], label='Close Price')
plt.title(f'{ticker} Stock Price')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.legend()
plt.show()

# 計(jì)算移動(dòng)平均線
data['MA50'] = data['Close'].rolling(window=50).mean()
data['MA200'] = data['Close'].rolling(window=200).mean()

# 繪制移動(dòng)平均線
plt.figure(figsize=(12, 6))
plt.plot(data['Close'], label='Close Price')
plt.plot(data['MA50'], label='50-Day MA')
plt.plot(data['MA200'], label='200-Day MA')
plt.title(f'{ticker} Stock Price with Moving Averages')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.legend()
plt.show()

在這個(gè)案例中,我們使用 yfinance 庫下載了蘋果公司(AAPL)的股票數(shù)據(jù),并繪制了收盤價(jià)走勢(shì)圖。接著,我們計(jì)算了 50 日和 200 日的移動(dòng)平均線,并將其與收盤價(jià)一起繪制,以便觀察價(jià)格趨勢(shì)。

通過上述示例,我們已經(jīng)看到了 NumPy 和 Matplotlib 在數(shù)據(jù)可視化中的強(qiáng)大能力。無論是簡(jiǎn)單的正弦波形還是復(fù)雜的等高線圖,都能輕松實(shí)現(xiàn)。希望這些基礎(chǔ)示例能夠幫助大家更好地理解和應(yīng)用這兩個(gè)庫。下一期我們繼續(xù)探索更多有趣的應(yīng)用!

責(zé)任編輯:趙寧寧 來源: 小白PythonAI編程
相關(guān)推薦

2024-07-01 08:51:19

可視化數(shù)據(jù)分析漏斗

2019-04-29 09:00:00

數(shù)據(jù)可視化JavaScript圖表庫

2020-12-17 09:40:01

Matplotlib數(shù)據(jù)可視化命令

2022-04-01 15:02:56

前端工具開發(fā)

2023-02-15 08:24:12

數(shù)據(jù)分析數(shù)據(jù)可視化

2021-11-09 08:15:18

Grafana 數(shù)據(jù)可視化運(yùn)維

2018-03-15 09:57:00

PythonMatplotlib數(shù)據(jù)可視化

2018-05-07 14:50:27

可視化數(shù)據(jù)散點(diǎn)圖

2024-12-25 16:35:53

2017-07-12 16:07:49

大數(shù)據(jù)數(shù)據(jù)可視化

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2020-08-14 10:45:26

Pandas可視化數(shù)據(jù)預(yù)處理

2022-06-29 09:54:17

Python數(shù)據(jù)可視化Altair

2022-07-11 13:30:08

Pandas數(shù)據(jù)編碼代碼

2022-04-20 15:10:55

pandas編碼函數(shù)

2024-12-24 12:00:00

Matplotlib可視化分析Python

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2017-12-11 16:25:25

2015-09-21 09:27:25

數(shù)據(jù)可視化錯(cuò)誤

2016-11-18 09:42:49

可視化數(shù)據(jù)分析優(yōu)選算法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)