我正在尝试从csv文件中绘制一个曲面,我在这里找到的例子没有帮助。事实上,我正在为我的数据寻找一个好的图表,我想,表面是一个好主意。 我的数据文件格式为:
v1_neighbors
TimeStamp, v2, v3, v4
1435756244367447, 1, 1, 1
1435756245460180, 1, 0, 1
1435756246476204, 1, 0, 1
1435756247478257, 1, 0, 1
...
这意味着在1435756245460180(第二个)中,节点v1
具有邻居节点v2
和v4,但不是v3
。这在数据中以1和0编码。这不是一个交易为每个节点与其邻居建立2D图表,但是将所有节点与其邻居放在一个图表中成为一个挑战。我要做的是一个3D图表,X和Y轴等于[v1, v2, v3, v4]
,Z轴等于TimeStamp
然后,为每个节点绘制一条3d线(例如蓝线代表邻居节点v1;节点v2的绿线邻居等)
答案 0 :(得分:0)
我不确定理解你的问题,但我会向你展示我解释的解决方案。首先,你需要存储文件中的信息,在这种情况下我使用了一个numpy数组,但是最好有一个numpy数组的字典......或者更好的东西。
我将[v1, v2, v3, v4]
编成[0, 1, 2, 3]
。每个时间戳只是z轴上的一个额外值。然后我检查了值是0还是1,看看你是否应该连接当前节点。
代码在
之下import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
timestamps = [r'$t_%i$'%i for i in range(1,5)]
data = np.zeros((4,3,4), dtype=int)
# Data for v1: The data you show
data[:,:,0] = np.array(
[[1, 1, 1],
[1, 0, 1],
[0, 1, 1],
[0, 0, 1]])
# Data for v2 to v4: random data
np.random.seed(seed=1)
data[:,:,1:] = np.random.randint(0, 2, size=(4, 3, 3))
fig = plt.figure()
ax = fig.gca(projection='3d')
colors = ['b', 'g', 'r', 'k']
for plane in range(4):
cols = range(4)
cols.pop(plane)
for row in range(4):
for num_col, col in enumerate(cols):
if data[row, num_col, plane]==1:
ax.plot([plane, 3], [0, col], 2*[row], c=colors[plane])
ax.set_xlabel('Initial')
ax.set_ylabel('Final')
ax.set_zlabel('Timestamp')
ax.set_xticks(range(4))
ax.set_yticks(range(4))
ax.set_zticks(range(4))
ax.set_xticklabels([r"$v_1$", r"$v_2$", r"$v_3$", r"$v_4$"])
ax.set_yticklabels([r"$v_1$", r"$v_2$", r"$v_3$", r"$v_4$"])
ax.set_zticklabels(timestamps)
plt.savefig('plot.png', dpi=300)
plt.show()
这给出了情节
答案 1 :(得分:0)
感谢您的回答@nicoguaro ....而且您说得对,表面不适合这些数据。事实上,我还没有意识到绘制数据的最佳方式。现在,我制作了一张散射3D图表。我无法发布图片,但我没有足够的声誉。
使用以下代码
##reading csv file
df = pd.read_csv('../../Fixed/csv-files/ColumnsZerosAndOnes/V4Neighbors.csv', skiprows=1)
## 'cropping' TimeStamp to more simple values
df['TimeStamp'] = df['TimeStamp'].apply(f)
## iteration step calculation
range_of_time = ((df['TimeStamp'].max() - df['TimeStamp'].min())/time_range)
###### coverting initial data into a 3 seconds time-merged data ####
columns = ['X', 'Y', 'Z']
df_v4 = pd.DataFrame(data=np.zeros((0,len(columns))), columns=columns)
base_value = 0
upper_value = base_value + time_range
for i in range(range_of_time):
tmp = df[((df['TimeStamp'] >= base_value) & (df['TimeStamp'] < upper_value))]
if len(tmp.index) != 0:
v1_tmp = tmp[tmp['v1'] == 1]
v2_tmp = tmp[tmp['v2'] == 1]
v3_tmp = tmp[tmp['v3'] == 1]
if len(v1_tmp.index) != 0:
df_v4 = df_v4.append({'X':node4, 'Y':node1, 'Z':base_value}, ignore_index=True)
pass
if len(v2_tmp.index) != 0:
df_v4 = df_v4.append({'X':node4, 'Y':node2, 'Z':base_value}, ignore_index=True)
pass
if len(v3_tmp.index) != 0:
df_v4 = df_v4.append({'X':node4, 'Y':node3, 'Z':base_value}, ignore_index=True)
pass
pass
base_value = upper_value
upper_value += time_range
pass
fig = plt.figure()
title = fig.suptitle('Emu Fixed Saved Neighbors', fontsize=25)
title.set_y(0.98)
fig.set_size_inches(15, 15, forward=True)
ax = fig.add_subplot(111, projection='3d')
## plotting
ax.scatter(df_v1['X'],df_v1['Y'],df_v1['Z'], c='r', marker='^')
ax.scatter(df_v2['X'],df_v2['Y'],df_v2['Z'], c='b', marker='o')
ax.scatter(df_v3['X'],df_v3['Y'],df_v3['Z'], c='g', marker='*')
ax.scatter(df_v4['X'],df_v4['Y'],df_v4['Z'], c='m', marker='s')
## axes labels
XYLabels = ['','v1','v2','v3','v4']
Zvalues = df_v1['Z'].unique()
Zvalues = Zvalues/10000
ax.set_xticklabels(XYLabels, minor=False)
ax.set_yticklabels(XYLabels, minor=False)
ax.set_zticklabels(Zvalues, minor=False)
# setting axes ticks
ax.yaxis.set_ticks(np.arange(0, 6, 1))
ax.xaxis.set_ticks(np.arange(0, 6, 1))
plt.show()