【问题标题】:matplotlib surface plot linewidth wrongmatplotlib曲面图线宽错误
【发布时间】:2014-04-28 17:41:30
【问题描述】:

我正在尝试使用 matplotlib 绘制曲面。 存在的问题是,即使我在代码中将线宽指定为零,show() 也会显示正确的不带线条的图。然而,生成的 pdf 中仍然有行。

谁能告诉我如何解决这个问题?

这是我用来绘图的代码

#!/usr/bin/env python


import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
import scipy.ndimage as ndimage


vmaxValue=400
#plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

fileName="test"
csvFile=fileName+".csv"
outputFile=fileName+".pdf"
pgfFile=fileName+".pgf"
data = np.genfromtxt(csvFile)

# Delete the first row and first column.
Z = np.delete(data, (0), axis=0)
Z = np.delete(Z, (0), axis=1)

Z2 = ndimage.gaussian_filter(Z, sigma=0.85, order=0)

X, Y = np.meshgrid(np.arange(1,len(Z[0])+1,1), np.arange(1,len(Z)+1,1))

surf = ax.plot_surface(X, Y, Z2, linewidth=0, cstride=1, rstride=1,  cmap=cm.coolwarm, antialiased=False, vmin=0, vmax=vmaxValue)

#plt.colorbar()
fig.colorbar(surf, ax=ax, shrink=0.75,pad=-0.05, aspect=15)

ax.set_zlim(0,vmaxValue)
ax.set_xlabel(r'$\alpha$')
ax.set_ylabel('processors')
ax.set_zlabel('Exploration Time(seconds)')
ax.view_init(20, -160)

fig.savefig(outputFile,format="pdf", bbox_inches='tight')

这里是 csv 文件 test.csv

Processors  graph1  graph2  graph3  graph4  graph5  graph6  graph7  graph8  graph9  graph10 graph11 graph12 graph13 graph14 graph15 graph16 graph17 graph18 graph19 graph20 graph21 graph22 graph23
1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
2   0   0   0   0   0   0   0   0   10  7   190 180 360 180 360 180 360 180 360 180 360 180 360
3   0   0   0   0   0   0   0   0   0   64  52  85  247 274 180 360 360 360 360 360 360 360 360
4   0   0   0   0   0   0   0   0   6   1   1   2   180 180 187 187 180 180 360 360 180 180 360
5   0   0   0   0   0   0   0   0   0   0   180 177 175 180 180 360 360 360 360 360 540 540 360
6   0   0   0   0   0   0   0   1   1   1   1   1   181 181 180 180 180 180 360 360 360 360 180
7   0   0   0   0   0   0   0   8   12  6   6   7   8   8   180 180 180 180 180 180 180 360 180
8   0   0   0   0   0   0   0   0   180 133 175 166 148 180 180 180 180 180 180 180 180 180 360
9   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180 180 180 180 180 360
10  0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180 180 180 180 360
11  0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180 180 180 360
12  0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180 180 180
13  0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180 180
14  0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180 180
15  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180 180
16  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180 180
17  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180 180
18  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180 180
19  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180 180 180
20  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 184 180
21  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180 180
22  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   180
23  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
24  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
25  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

谢谢

【问题讨论】:

    标签: matplotlib plot


    【解决方案1】:

    显然这与您的 pdf 抗锯齿有关。

    如果您不介意一些额外的计算时间,this answer 建议您可以通过多次绘制相同的图形来摆脱它,

    for i in range(k):
       ax.plot_surface(X, Y, Z2, linewidth=0, cstride=1, rstride=1,  cmap=cm.coolwarm, antialiased=False, vmin=0, vmax=vmaxValue)
    

    注意k=2 对我来说效果很好。我不会说每次这样做都会使文件大小翻倍,但文件大小会显着增加

    如果您觉得更有冒险精神,您可以查看this answer,了解与contourf 相同的问题。总结:

    for c in cnt.collections:
       c.set_edgecolor("face")
    

    不幸的是matplotlib 3D Objects 没有属性collections 所以它不会马上工作,但希望这能给你一个启发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多