簡體   English   中英

Matplotlib 3D 瀑布 Plot 帶彩色高度

[英]Matplotlib 3D Waterfall Plot with Colored Heights

我正在嘗試使用 Python 和 Matplotlib 可視化 3D 中的數據集,該數據集由 xz 數據的時間序列(沿 y 方向)組成。

I'd like to create a plot like the one below (which was made in Python: http://austringer.net/wp/index.php/2011/05/20/plotting-a-dolphin-biosonar-click-train / ),但顏色隨 Z 變化 - 即,為了清楚起見,強度由顏色圖和峰高顯示。

在此處輸入圖像描述

顯示 Z 中顏色圖的示例是(顯然是使用 MATLAB 制作的): 在此處輸入圖像描述

可以使用 MATLAB 中的瀑布 plot 選項創建此效果,但我知道在 Python 中沒有直接等效的效果。

我也嘗試過在 Python(下)中使用 plot_surface 選項,它工作正常,但我想“強制”在表面上運行的線只在 x 方向上(即讓它看起來更像是一個堆疊的時間系列比表面)。 這可能嗎? 在此處輸入圖像描述

非常歡迎任何幫助或建議。 謝謝。

我已經生成了一個在matplotlib中復制matlab瀑布行為的函數,但我不認為它是性能方面的最佳解決方案。

我從matplotlib文檔中的兩個例子開始: 多色線3d圖中的多行 從這些例子中,我只看到可以根據示例后面的z值繪制顏色因給定顏色圖而變化的線,這是重新整形輸入數組以按2點的線段繪制線並將線段的顏色設置為兩點之間的z平均值。

因此,給定輸入矩陣n,m矩陣XYZ ,函數在n,m之間的最小維度上循環以繪制每條線,如在示例中,通過2個點段,其中通過分段繪制的重新整形使用與示例相同的代碼重新整形數組。

def waterfall_plot(fig,ax,X,Y,Z):
    '''
    Make a waterfall plot
    Input:
        fig,ax : matplotlib figure and axes to populate
        Z : n,m numpy array. Must be a 2d array even if only one line should be plotted
        X,Y : n,m array
    '''
    # Set normalization to the same values for all plots
    norm = plt.Normalize(Z.min().min(), Z.max().max())
    # Check sizes to loop always over the smallest dimension
    n,m = Z.shape
    if n>m:
        X=X.T; Y=Y.T; Z=Z.T
        m,n = n,m

    for j in range(n):
        # reshape the X,Z into pairs 
        points = np.array([X[j,:], Z[j,:]]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)        
        lc = LineCollection(segments, cmap='plasma', norm=norm)
        # Set the values used for colormapping
        lc.set_array((Z[j,1:]+Z[j,:-1])/2)
        lc.set_linewidth(2) # set linewidth a little larger to see properly the colormap variation
        line = ax.add_collection3d(lc,zs=(Y[j,1:]+Y[j,:-1])/2, zdir='y') # add line to axes

    fig.colorbar(lc) # add colorbar, as the normalization is the same for all, it doesent matter which of the lc objects we use

因此,使用與matplotlib曲面圖相同的輸入矩陣可以輕松生成看起來像matlab瀑布的圖:

import numpy as np; import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from mpl_toolkits.mplot3d import Axes3D

# Generate data
x = np.linspace(-2,2, 500)
y = np.linspace(-2,2, 40)
X,Y = np.meshgrid(x,y)
Z = np.sin(X**2+Y**2)
# Generate waterfall plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
waterfall_plot(fig,ax,X,Y,Z) 
ax.set_xlabel('X') ; ax.set_xlim3d(-2,2)
ax.set_ylabel('Y') ; ax.set_ylim3d(-2,2)
ax.set_zlabel('Z') ; ax.set_zlim3d(-1,1)

瀑布情節

該函數假定在生成meshgrid時, x數組是最長的,默認情況下,行具有固定的y,其x坐標是變化的。 但是,如果y維的大小較大,則矩陣被轉置,生成具有固定x的線。 因此,生成反轉大小的網格網格( len(x)=40len(y)=500 )會產生:

非默認瀑布圖

pandas dataframe 以 x 軸為第一列,每個光譜為另一列

offset=0
for c in s.columns[1:]:
    plt.plot(s.wavelength,s[c]+offset)
    offset+=.25
    plt.xlim([1325,1375])

在此處輸入圖像描述

我怎樣才能重現這個美麗的 plot,線條逐年變化,基於每條線的特定顏色標准(6 類),如 Jeff 的 plot?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM