python matplotlib隐形点

时间:2015-06-06 18:21:17

标签: python matplotlib

我正在学习matplolib。我尝试在表面上显示特定点,我得到以下结果:

We can hardly see the point here Here we can't

这两个截图都来自同一个数字。在第一个,我们很难看到我想要显示的点,而不是第二个。我想让它出现在每一个上面。这是我的代码:

import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

hours, pauses, results = [], [], []

with open('./data.txt', 'r') as f:
    for line in f:
        value = line.split()
        for i, l in enumerate([hours, pauses, results]):
            l.append(int(value[i]))

for l in [hours, pauses, results]:
    l = np.asarray(l, dtype=float)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

m = len(hours)

WP_init = 5 * np.random.rand()
WH_init = 5 * np.random.rand()
Z_init = 0
for i in range(m):
    Z_init += 1. / (2 * m) * (WH_init * hours[i] + WP_init * pauses[i] - results[i])**2
ax.scatter(WH_init, WP_init, Z_init, s=40, c='r', marker='o')


WH = np.arange(-5, 5, 0.25)
WP = np.arange(-5, 5, 0.25)
WH, WP = np.meshgrid(WH, WP)
Z = np.zeros(np.shape(WP))
for i in range(m):
    Z += 1. / (2 * m) * (WH * hours[i] + WP * pauses[i] - results[i])**2

ax.plot_surface(WH, WP, Z, rstride=4, cstride=4, cmap=cm.coolwarm)

plt.show()

你知道我怎么解决这些问题吗?

由于

编辑:这是data.txt的内容:
12 0 13
12 8 18
10 0 14
10 4 16
8 0 14
8 2 15
8 8 8
6 0 13
6 3 11
4 0 8
4 4 4
2 2 2
0 0 0

1 个答案:

答案 0 :(得分:4)

您可以将曲面的alpha设置为小于1的值,以使其不透明度不会完全遮盖红点:

ax.plot_surface(WH, WP, Z, rstride=4, cstride=4, cmap=cm.coolwarm, alpha=0.5)

enter image description here