Matplotlib的rstride,cstride弄乱了plot_surface 3D图中的颜色图? [英] Matplotlib's rstride, cstride messes up color maps in plot_surface 3D plot?

查看:47
本文介绍了Matplotlib的rstride,cstride弄乱了plot_surface 3D图中的颜色图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3595 个 .csv 文件的大型数据集,其中包含 1252 对 x,y 元组.每个文件代表一个时间范围.这些是使用 plot_surf() 绘制的.我发现,在绘制数据时,默认情况下将对我的数据进行筛选(关于文件或时间范围),步长为10,这就是为什么我需要指定 rstride = 1,cstride = 1 在我的艺术家中以绘制所有内容.

I have a large dataset consisting of 3595 .csv files containing 1252 pairs of x,y tuples. Each file represents a time frame. These are plottet using plot_surf(). I found out, that my data will be sieved (in regards to the files, or time frames) by default with a step of 10 when plotting my data, which is why I need to specify rstride=1, cstride=1 in my artist in order to plot everything.

当我这样做时,我偶然发现了我之前遇到的问题的解决方案:默认情况下,绘图显示表面有规则的间隙,这不是所提供数据的结果.此外,未正确使用颜色图jet".这些问题可以在下图中看到.

When I did this, I accidentally discovered the solution to a problem I faced earlier: By default the plot showed regular gaps in the surface, which are not a consequence of the data provided. Further, the colormap "jet" was not used correctly. These issues can be seen in the plot below.

将此与情节实际应有的样子进行比较:

Compare this to how the plot should actually look like:

我在代码中所做的全部更改是plot_surf函数的args rstride和cstride,它伴随着非常非常长的执行时间.但我确实得到了正确的结果.

All I changed in my code was the args rstride and cstride of the plot_surf function, which was accompanied by a very, very long execution time. But I did get the correct result.

所以我的问题是:有什么用?为什么这会突然起作用?为什么没有间隙的颜色图/图对默认跨度不起作用?

So my question is: What gives? Why does this work suddenly? Why won't the colormap/plot without gaps work for the default stride?

这是我的代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.ticker as tkr
import numpy as np
import glob
import pandas as pd

Files = glob.glob('*.xy')
dtime = 1

Z = np.array([pd.read_csv(file,
                          decimal=',',
                          delim_whitespace=True,
                          header=None,
                          names=['2theta','I'])['I']
                          for num, file in enumerate(Files)
                          if num < len(Files)])



X = np.array([pd.read_csv(Files[0],
                          decimal=',',
                          delim_whitespace=True,
                          header=None,
                          names=['2theta','I'])['2theta']])

Y = np.array([[t*dtime for t in range(0,len(Files))]])

X, Y = np.meshgrid(X, Y)


fig = plt.figure(figsize=(7,5))
ax = fig.gca(projection='3d')


# Plot the surface.
surf = ax.plot_surface(X,
                       Y,
                       Z,
                       cmap=cm.jet,
                       rstride=1,
                       cstride=1,
                       vmin=np.amin(Z),
                       vmax=np.amax(Z),
                       linewidth=0,
                       antialiased=True)

ax.set_ylabel(r'$t \quad / \quad$ s',
              labelpad=7)

ax.set_xlabel(r'$2\theta \quad / \quad °$',
              labelpad=7)

ax.set_zlabel('$I$ in a.u.',
              labelpad=7)


ax.xaxis.set_major_locator(tkr.AutoLocator())
ax.yaxis.set_major_locator(tkr.AutoLocator())
ax.zaxis.set_major_locator(tkr.AutoLocator())

ax.get_xaxis().get_major_formatter().set_useOffset(True)
ax.get_xaxis().get_major_formatter().set_useOffset(True)
ax.get_xaxis().get_major_formatter().set_useOffset(True)

fig.colorbar(surf, shrink=0.7, aspect=20, pad=0.12)

plt.tight_layout()
plt.savefig('3D.png', dpi=300, bbox='tight')

推荐答案

看看 rstride和cstride kwarg设置用于采样输入数据以生成图形的步幅.如果传入1k x 1k数组,则跨步的默认值将导致绘制100x100的网格.默认为 10.如果同时提供了 stride 和 count kwargs(参见下一节),则会引发 ValueError.

The rstride and cstride kwargs set the stride used to sample the input data to generate the graph. If 1k by 1k arrays are passed in, the default values for the strides will result in a 100x100 grid being plotted. Defaults to 10. Raises a ValueError if both stride and count kwargs (see next section) are provided.

这应该可以回答您的问题:如果 rstridecstride 不是 1,则并非所有点都用于绘制曲面.
这样可以节省时间,因为要绘制的点越多,计算图所需的时间就越长.
但同时,如果您的表面具有很高的可变性,则在图中跳过 9 个点超过 10 个点会导致不同的图片.

This should answer your question: if rstride and cstride are not 1, not all the points are used to draw the surface.
This saves time, because the more the points to plot, the longer the time needed to compute the plot.
But at the same time, if your surface has a high variability, skipping 9 points over 10 in the plot will result in a different picture.

这篇关于Matplotlib的rstride,cstride弄乱了plot_surface 3D图中的颜色图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆