KF *_*uss 4 python graph matplotlib
我正在尝试使用 Python 和 Matplotlib 创建 Origin 制作的瀑布图(见下图)。
一般方案对我来说很有意义,您从 2D 矩阵开始,就像您想要制作曲面图一样,然后您可以遵循StackOverflow 问题中显示的任何方法(此处)。这个想法是将矩阵的每条线绘制为 3D 空间中的一条单独的曲线。
此 matplotlib 方法会生成如下图所示的图:

我遇到的困难是,Origin 图中清晰的透视感在 matplotlib 版本中丢失了。你可以说这部分是由于相机角度造成的,但我认为更重要的是,它来自于较远的线“前面”出现的较近的线。
我的问题是,如何在 Matplotlib 中正确模仿 Origin 中的瀑布图并具有透视效果?我真的不明白这两个情节有什么不同,所以即使定义确切的问题也很困难。
更新:由于您现在已经更新了您的问题以更清楚地了解您想要什么,让我演示三种不同的方法来绘制此类数据,这些方法都有很多优点和缺点。\n一般要点(至少对我来说)!)这在 3D 中matplotlib很糟糕,尤其是在创建可发布的图形时(同样,我个人的观点,您的里程可能会有所不同。)
我做了什么:我使用了您发布的第二张图片背后的原始数据。在所有情况下,我都使用zorder并添加了多边形数据(在 2D: 中fill_between(),在 3D: 中PolyCollection)来增强“3D 效果”,即启用“在彼此前面绘制”。下面的代码显示:
plot_2D_a()使用颜色来指示角度,从而保持原始 y 轴;尽管从技术上讲,现在只能用于读出最前面的线图,但它仍然为读者提供了对 y 比例的“感觉”。plot_2D_b()删除不必要的刺/蜱,而是添加角度作为文本标签;这最接近您发布的第二张图片plot_3D()用于mplot3d制作“3D”图;虽然现在可以旋转它来分析数据,但在尝试缩放时它会中断(至少对我来说),产生截止数据和/或隐藏轴。最后,实现瀑布图的方法有很多matplotlib,你必须自己决定你想要什么。就我个人而言,我可能plot_2D_a()大部分时间都会使用它,因为它允许在或多或少的“所有 3 个维度”上轻松重新缩放,同时还保留适当的轴(+颜色条),以便读者在发布后获得所有相关信息某处作为静态图像。
代码:
\n\nimport pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib.collections import PolyCollection\nimport numpy as np\n\n\ndef offset(myFig,myAx,n=1,yOff=60):\n dx, dy = 0., yOff/myFig.dpi \n return myAx.transData + mpl.transforms.ScaledTranslation(dx,n*dy,myFig.dpi_scale_trans)\n\n## taken from \n## http://www.gnuplotting.org/data/head_related_impulse_responses.txt\ndf=pd.read_csv(\'head_related_impulse_responses.txt\',delimiter="\\t",skiprows=range(2),header=None)\ndf=df.transpose()\n\ndef plot_2D_a():\n """ a 2D plot which uses color to indicate the angle"""\n fig,ax=plt.subplots(figsize=(5,6))\n sampling=2\n thetas=range(0,360)[::sampling]\n\n cmap = mpl.cm.get_cmap(\'viridis\')\n norm = mpl.colors.Normalize(vmin=0,vmax=360)\n\n for idx,i in enumerate(thetas):\n z_ind=360-idx ## to ensure each plot is "behind" the previous plot\n trans=offset(fig,ax,idx,yOff=sampling)\n\n xs=df.loc[0]\n ys=df.loc[i+1]\n\n ## note that I am using both .plot() and .fill_between(.. edgecolor="None" ..) \n # in order to circumvent showing the "edges" of the fill_between \n ax.plot(xs,ys,color=cmap(norm(i)),linewidth=1, transform=trans,zorder=z_ind)\n ## try alpha=0.05 below for some "light shading"\n ax.fill_between(xs,ys,-0.5,facecolor="w",alpha=1, edgecolor="None",transform=trans,zorder=z_ind)\n\n cbax = fig.add_axes([0.9, 0.15, 0.02, 0.7]) # x-position, y-position, x-width, y-height\n cb1 = mpl.colorbar.ColorbarBase(cbax, cmap=cmap, norm=norm, orientation=\'vertical\')\n cb1.set_label(\'Angle\')\n\n ## use some sensible viewing limits\n ax.set_xlim(-0.2,2.2)\n ax.set_ylim(-0.5,5)\n\n ax.set_xlabel(\'time [ms]\')\n\ndef plot_2D_b():\n """ a 2D plot which removes the y-axis and replaces it with text labels to indicate angles """\n fig,ax=plt.subplots(figsize=(5,6))\n sampling=2\n thetas=range(0,360)[::sampling]\n\n for idx,i in enumerate(thetas):\n z_ind=360-idx ## to ensure each plot is "behind" the previous plot\n trans=offset(fig,ax,idx,yOff=sampling)\n\n xs=df.loc[0]\n ys=df.loc[i+1]\n\n ## note that I am using both .plot() and .fill_between(.. edgecolor="None" ..) \n # in order to circumvent showing the "edges" of the fill_between \n ax.plot(xs,ys,color="k",linewidth=0.5, transform=trans,zorder=z_ind)\n ax.fill_between(xs,ys,-0.5,facecolor="w", edgecolor="None",transform=trans,zorder=z_ind)\n\n ## for every 10th line plot, add a text denoting the angle. \n # There is probably a better way to do this.\n if idx%10==0:\n textTrans=mpl.transforms.blended_transform_factory(ax.transAxes, trans)\n ax.text(-0.05,0,u\'{0}\xc2\xba\'.format(i),ha="center",va="center",transform=textTrans,clip_on=False)\n\n ## use some sensible viewing limits\n ax.set_xlim(df.loc[0].min(),df.loc[0].max())\n ax.set_ylim(-0.5,5)\n\n ## turn off the spines\n for side in ["top","right","left"]:\n ax.spines[side].set_visible(False)\n ## and turn off the y axis\n ax.set_yticks([])\n\n ax.set_xlabel(\'time [ms]\')\n\n#--------------------------------------------------------------------------------\ndef plot_3D():\n """ a 3D plot of the data, with differently scaled axes"""\n fig=plt.figure(figsize=(5,6))\n ax= fig.gca(projection=\'3d\')\n\n """ \n adjust the axes3d scaling, taken from https://stackoverflow.com/a/30419243/565489\n """\n # OUR ONE LINER ADDED HERE: to scale the x, y, z axes\n ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1, 2, 1, 1]))\n\n sampling=2\n thetas=range(0,360)[::sampling]\n verts = []\n count = len(thetas)\n\n for idx,i in enumerate(thetas):\n z_ind=360-idx\n\n xs=df.loc[0].values\n ys=df.loc[i+1].values\n\n ## To have the polygons stretch to the bottom, \n # you either have to change the outermost ydata here, \n # or append one "x" pixel on each side and then run this.\n ys[0] = -0.5 \n ys[-1]= -0.5\n\n verts.append(list(zip(xs, ys))) \n\n zs=thetas\n\n poly = PolyCollection(verts, facecolors = "w", edgecolors="k",linewidth=0.5 )\n ax.add_collection3d(poly, zs=zs, zdir=\'y\')\n\n ax.set_ylim(0,360)\n ax.set_xlim(df.loc[0].min(),df.loc[0].max())\n ax.set_zlim(-0.5,1)\n\n ax.set_xlabel(\'time [ms]\')\n\n# plot_2D_a()\n# plot_2D_b()\nplot_3D()\nplt.show()\nRun Code Online (Sandbox Code Playgroud)\n
实际上,不久前我在为正在写的论文创建绘图时遇到了这个问题。我基本上得到了与 Asmus 相同的答案,所以我不会向您介绍如何实现它的细节,因为这已经被涵盖了,但是我添加了具有高度相关的颜色映射而不是角度相关的颜色映射的功能。下面的例子:

这可能是也可能不是您想要添加的内容,但它有助于了解数据的实际 y 值,在创建这样的瀑布图时混合 y 轴和 z 轴时会丢失该值。
这是我用来生成它的代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
# generate data: sine wave (x-y) with 1/z frequency dependency
Nx = 200
Nz = 91
x = np.linspace(-10, 10, Nx)
z = 0.1*np.linspace(-10, 10, Nz)**2 + 4
w = 2*np.pi # omega
y = np.zeros((Nx, Nz))
for i in range(Nz):
y[:, i] = np.cos(w*x/z[i]**0.5)/z[i]**0.2
# create waterfall plot
fig = plt.figure()
ax = fig.add_subplot(111)
for side in ['right', 'top', 'left']:
ax.spines[side].set_visible(False)
# some usefull parameters
highest = np.max(y)
lowest = np.min(y)
delta = highest-lowest
t = np.sqrt(abs(delta))/10 # a tuning parameter for the offset of each dataset
for i in np.flip(range(Nz)):
yi_ = y[:,i] # the y data set
yi = yi_ + i*t # the shifted y data set used for plotting
zindex = Nz-i # used to set zorder
# fill with white from the (shifted) y data down to the lowest value
# for good results, don't make the alpha too low, otherwise you'll get confusing blending of lines
ax.fill_between(x, lowest, yi, facecolor="white", alpha=0.5, zorder=zindex)
# cut the data into segments that can be colored individually
points = np.array([x, yi]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# Create a continuous norm to map from data points to colors
norm = plt.Normalize(lowest, highest)
lc = LineCollection(segments, cmap='plasma', norm=norm)
# Set the values used for colormapping
lc.set_array(yi_)
lc.set_zorder(zindex)
lc.set_linewidth(1)
line = ax.add_collection(lc)
# print text indicating angle
delta_x = max(x)-min(x)
if (i)%10==0:
ax.text(min(x)-5e-2*delta_x, t*i, "$\\theta=%i^\\circ$"%i, horizontalAlignment="right")
# set limits, as using LineCollection does not automatically set these
ax.set_ylim(lowest, highest + Nz*t)
ax.set_xlim(-10, 10)
fig.colorbar(line, ax=ax)
plt.yticks([])
ax.yaxis.set_ticks_position('none')
fig.savefig("waterfall_plot_cmap")
Run Code Online (Sandbox Code Playgroud)
我从这里的官方 matplotlib 示例中找到了如何从本教程中获取高度映射
如果有人感兴趣,我也将生成黑白版本的代码上传到我的github