我正在绘制星团中的位置,我的数据位于具有x,y,z位置的数据帧以及时间索引中.
我能够制作一个三维散点图,并试图产生一个旋转图 - 我有点成功,但在动画API中苦苦挣扎.
如果我的"update_graph"函数只返回一个新的ax.scatter(),那么旧的一个会保留绘制,除非我重建整个图形.这似乎效率低下.同样,我必须设置我的间隔相当高或我的动画"跳过"每隔一帧,所以它说我的表现相当糟糕.最后我被迫使用"blit = False",因为我无法获得3d散点图的迭代器.显然"graph.set_data()"不起作用,我可以使用"graph.set_3d_properties"但只允许我使用新的z坐标.
所以我拼凑了一个cluuge--(我使用的数据是 https://www.kaggle.com/mariopasquato/star-cluster-simulations 滚动到底部)
另外我只绘制100个点(data = data [data.id <100])
我的(工作)代码如下:
def update_graph(num):
ax = p3.Axes3D(fig)
ax.set_xlim3d([-5.0, 5.0])
ax.set_xlabel('X')
ax.set_ylim3d([-5.0, 5.0])
ax.set_ylabel('Y')
ax.set_zlim3d([-5.0, 5.0])
ax.set_zlabel('Z')
title='3D Test, Time='+str(num*100)
ax.set_title(title)
sample=data0[data0['time']==num*100]
x=sample.x
y=sample.y
z=sample.z
graph=ax.scatter(x,y,z)
return(graph)
fig = plt.figure()
ax = p3.Axes3D(fig)
# Setting the axes properties
ax.set_xlim3d([-5.0, 5.0])
ax.set_xlabel('X')
ax.set_ylim3d([-5.0, 5.0])
ax.set_ylabel('Y')
ax.set_zlim3d([-5.0, 5.0])
ax.set_zlabel('Z')
ax.set_title('3D Test')
data=data0[data0['time']==0]
x=data.x
y=data.y
z=data.z
graph=ax.scatter(x,y,z)
# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_graph, 19,
interval=350, blit=False)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我有一个大文件(2GB)的分类数据(大多数是"Nan" - 但在这里和那里填充了实际值)太大而无法读入单个数据帧.我有一个相当困难的时间想出一个对象来存储每列的所有唯一值(这是我的目标 - 最终我需要将其分解为建模)
我最后做的是将文件以块的形式读入数据帧,然后获取每列的唯一值并将它们存储在列表列表中.我的解决方案的工作,但似乎大多数非Python的 - 有没有在Python做到这一点(版本3.5)一个更清洁的方式.我知道列数(~2100).
import pandas as pd
#large file of csv separated text data
data=pd.read_csv("./myratherlargefile.csv",chunksize=100000, dtype=str)
collist=[]
master=[]
i=0
initialize=0
for chunk in data:
#so the first time through I have to make the "master" list
if initialize==0:
for col in chunk:
#thinking about this, i should have just dropped this col
if col=='Id':
continue
else:
#use pd.unique as a build in solution to get unique values
collist=chunk[col][chunk[col].notnull()].unique().tolist()
master.append(collist)
i=i+1
#but after first loop just …Run Code Online (Sandbox Code Playgroud) 在过去的 24 小时里,我一直在为此苦苦挣扎。我正在做一个神经网络——将图像存储在 4D 阵列中。数组的第一个索引基本上是“样本”,也就是样本 1、2、3 等。维度 2、3、4 是 128x128 x3 rgb 图片。现在在这个过程中,我拍摄输入图片(不是 128x128)并重新缩放它们。但是当我选择一个样本时,它混合了所有的颜色通道。所以我试图找出问题出在哪里。
如果我只是调整图片大小并将数字数组 (128x128x3) 分配给一个变量,那么一切都是“正常的”。如果我将“子阵列”分配给更大的 4 维阵列,颜色通道就会混淆。但是,我可以通过图片从 255 中减去相同部分的数组切片来恢复原始图像。
这是一个代码片段,其中包含原始 (1)、调整大小 (2)、颜色通道混合 (3)、恢复 (4)。
我知道 open cv 和 pyplot.imshow() 使用不同的颜色通道,但事实似乎是将图片存储在更大的数组中是导致切换的原因 - 这让我感到困惑。一些指导将不胜感激。
我也可以“减去”数组(img2-train[0])并得到一个全零的数组)。那部分真的很令人困惑。它们是相同的数字,但 imshow() 给出了 2 个完全不同的图像。
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(<path to your pic>)
img2 = cv2.resize(img, (128, 128))
train = np.ndarray(shape=(1,128, 128,3))
plt.subplot(1,4,1)
plt.imshow(img)
plt.subplot(1,4,2)
plt.imshow(img2)
plt.subplot(1,4,3)
train[0] = img2
plt.imshow(train[0])
plt.subplot(1,4,4)
plt.imshow(255-train[0])
plt.show()
Run Code Online (Sandbox Code Playgroud)