有没有一个可以像MATLAB一样绘制瀑布图的python模块?我在谷歌上搜索了“麻木瀑布”、“小瀑布”和“matplotlib瀑布”,但什么也没找到。
发布于 2012-06-26 22:45:45
看一看mplot3d
# copied from
# http://matplotlib.sourceforge.net/mpl_examples/mplot3d/wire3d_demo.py
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()

我不知道怎样才能得到像Matlab一样好的结果。
如果你想要更多,你也可以看看MayaVi:http://mayavi.sourceforge.net/
发布于 2014-08-28 20:43:02
维基百科类型的Waterfall chart也可以像这样获得:
import numpy as np
import pandas as pd
def waterfall(series):
df = pd.DataFrame({'pos':np.maximum(series,0),'neg':np.minimum(series,0)})
blank = series.cumsum().shift(1).fillna(0)
df.plot(kind='bar', stacked=True, bottom=blank, color=['r','b'])
step = blank.reset_index(drop=True).repeat(3).shift(-1)
step[1::3] = np.nan
plt.plot(step.index, step.values,'k')
test = pd.Series(-1 + 2 * np.random.rand(10), index=list('abcdefghij'))
waterfall(test)发布于 2018-06-22 09:00:14
我已经在matplotlib中生成了一个复制matlab瀑布行为的函数。这就是:
它将3D形状生成为许多独立和平行的2D曲线,它的颜色来自z values中的色彩映射表
我从matplotlib文档中的两个示例开始:multicolor lines和multiple lines in 3d plot。从这些示例中,我只看到可以根据给定的色彩映射表根据其z值绘制颜色变化的线条。下面的示例是重塑输入数组,通过两点的线段绘制线条,并将线段的颜色设置为这两点之间的z平均值。
因此,给定输入矩阵n,m矩阵X、Y和Z,该函数在n,m之间的最小维度上循环,以将每条瀑布图独立线绘制为如上所述的两点线段的线集。
def waterfall_plot(fig,ax,X,Y,Z,**kwargs):
'''
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
kwargs : kwargs are directly passed to the LineCollection object
'''
# 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)
# The values used by the colormap are the input to the array parameter
lc = LineCollection(segments, cmap='plasma', norm=norm, array=(Z[j,1:]+Z[j,:-1])/2, **kwargs)
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
ax.auto_scale_xyz(X,Y,Z) # set axis limits因此,可以使用与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, 60)
X,Y = np.meshgrid(x,y)
Z = np.sin(X**2+Y**2)-.2*X
# Generate waterfall plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
waterfall_plot(fig,ax,X,Y,Z,linewidth=1.5,alpha=0.5)
ax.set_xlabel('X'); ax.set_ylabel('Y'); ax.set_zlabel('Z')
fig.tight_layout()

该函数假定在生成网格时,x数组是最长的,默认情况下,直线的y是固定的,x坐标是变化的。但是,如果y数组的大小较长,则会转置矩阵,生成具有固定x的行。因此,生成大小颠倒的网格(len(x)=60和len(y)=500)会产生以下结果:

要了解**kwargs参数的可能性,请参阅LineCollection class documantation及其set_ methods。
https://stackoverflow.com/questions/11209646
复制相似问题