Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从TCP数据绘制散点图_Python_Arrays_Matplotlib - Fatal编程技术网

Python 如何从TCP数据绘制散点图

Python 如何从TCP数据绘制散点图,python,arrays,matplotlib,Python,Arrays,Matplotlib,我被困在一个问题中,尝试了这个社区的许多解决方案。但我无法找到解决我问题的办法 目前,我能够创建一个程序,通过该程序,我能够连续地从阵列中的TCP套接字收集点。我能够接收来自插座的连续输出。 现在我想从这些点可视化散点图3D图形。但我无法查看图表。无论这个社区有什么解决方案,我都试过了。但我收到错误,无法得到结果。 以下是我从TCP收集数据的代码: from __future__ import print_function from mpl_toolkits.mplot3d import Axe

我被困在一个问题中,尝试了这个社区的许多解决方案。但我无法找到解决我问题的办法

目前,我能够创建一个程序,通过该程序,我能够连续地从阵列中的TCP套接字收集点。我能够接收来自插座的连续输出。 现在我想从这些点可视化散点图3D图形。但我无法查看图表。无论这个社区有什么解决方案,我都试过了。但我收到错误,无法得到结果。 以下是我从TCP收集数据的代码:

from __future__ import print_function
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import
import matplotlib.pyplot as plt
import numpy as np
import socket
import sys
import time
from time import sleep

start = time.time()
def comm_dists(ip, port):

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (ip, port)
    print(sys.stderr, 'connecting to %s port %s' % server_address)
    sock.connect(server_address)
    print("Connected")
    try:

        # Send data
        message = b'\x02\x02\x02\x02\x00\x00\x00\x10sMI 0 3 F4724744 ' #This will initiate the scanning process
        sock.sendall(message)
        print(sys.stderr, 'sending "%s"' % message)
        print('sent "%s"' % message)
        data = sock.recv(5000)
        sleep(0.05)
        n = 52
        while True:
            data = sock.recv(5000)
            data = data.decode("utf-8", errors='ignore')
            data = data.split()
            integer_data = [int.from_bytes(bytes.fromhex(item), 'big') for item in data[:-8]] #Convert the received values in Integer.
            print('received "%s"' % integer_data) #Print the data received from TCP socket 

    finally:
        print(sys.stderr, 'closing socket')
    return data

i=0
dados_csv = []

while i < int(1):
    data = comm_dists('192.168.0.1', 2111) #IP and port of the socket

    i=i+1
我试图为我的程序实现以下代码。但它不起作用。我还尝试了这个社区的其他几种解决方案

import matplotlib.pyplot as plt
import numpy as np


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

# Create the mesh in polar coordinates and compute corresponding Z.
r = np.linspace(0, 1.25, 50)
p = np.linspace(0, 2*np.pi, 50)
R, P = np.meshgrid(r, p)
Z = ((R**2 - 1)**2)

# Express the mesh in the cartesian system.
X, Y = R*np.cos(P), R*np.sin(P)

# Plot the surface.
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

# Tweak the limits and add latex math labels.
ax.set_zlim(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')

plt.show()

但我无法在3d plot中绘制上述点。请帮忙画这张图。提前谢谢

编辑: θ范围为55度至125度,分辨率为0.25度。
i、 e第一个点在55度,第二个点在55.25度,第三个点在55.5度…..第n个点在125度,我已经将传感器固定在墙上,所以φ固定在90度。

最后给出的代码不会运行。正如我在评论中提到的,您必须使用mpl_toolkits.mplot3d。从中,您可以直接获得:

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



fig = plt.figure()
ax = fig.gca(projection='3d')

# Create the mesh in polar coordinates and compute corresponding Z.
r = np.linspace(0, 1.25, 50)
p = np.linspace(0, 2*np.pi, 50)
R, P = np.meshgrid(r, p)
Z = ((R**2 - 1)**2)

# Express the mesh in the cartesian system.
X, Y = R*np.cos(P), R*np.sin(P)

# Plot the surface.
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

# Tweak the limits and add latex math labels.
ax.set_zlim(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')

plt.show()
它运行并提供:

编辑:根据您的评论,我认为您需要在上述代码中替换:

r=整数_数据 p=np.linspace55,125,列宁泰格数据*np.pi/180或p=np.arange55,125+0.25,0.25*np.pi/180。在第一个选项中,您必须检查间距是否为.25度。最后,您必须检查整型_数据的长度是否对应于p的长度。如果没有,则角度范围/步长相对于要绘制的数据是错误的。
除非您提供有关所获得错误及其链接变量的具体信息,否则我无法提供更多帮助。

您需要使用mplot3d而不是常规的matplotlib.pyplot。看一看基础课程的教程syntax@Liris我也试过这么做。但我没能得到我的解决方案。我得到了错误AttributeError:'int'对象没有属性'cos',上面的异常是以下异常的直接原因:@Liris,你能帮我在上面的代码中提出建议吗?什么是integer_数据?这个xyz位置是平坦的吗?绘制tcp位置的代码是什么?一般来说,ax.scatterx,y,z应该可以工作。正如上面所讨论的,这里提到了输出。我收到的数据是整数形式的。我的θ范围在55度到125度之间,而不是r=np.linspace0,1.25,50,我必须输入从TCP端口接收到的值。我使用**data=sock.recv5000**获取值我接收的值是十六进制格式的。因此,我使用integer_data=[int.from_bytesbytes.fromhexitem,'big'表示数据中的项[:-8]]将十六进制值转换为int格式,所以现在我必须在建议的代码中用这个值代替r。你能帮我个忙吗?我用p=np.linspace0,2*np.pi,50来代替p=np.linspace11*np.pi/36,25*np.pi/36,0.25,因为角度的范围是55度到125度,分辨率是0.25度如果你知道你必须用什么来代替r和p,你不做什么?你得到了什么错误?顺便说一下,您使用的语法p=np.linspace11*np.pi/36,25*np.pi/36,0.25,请查看文档中的np.linspace。如果你在评论中一个接一个地给出你的代码,我就帮不了你了。请编辑您的初始帖子,张贴给出错误的代码和您获得的错误,这两个错误都是在使用我的初始答案和评论进行更正后发布的。这个错误可能与一个变量有关,它可以用来描述对象类型、长度等。@Liris我不知道他想要什么了。。。。
from mpl_toolkits.mplot3d import Axes3D  
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np



fig = plt.figure()
ax = fig.gca(projection='3d')

# Create the mesh in polar coordinates and compute corresponding Z.
r = np.linspace(0, 1.25, 50)
p = np.linspace(0, 2*np.pi, 50)
R, P = np.meshgrid(r, p)
Z = ((R**2 - 1)**2)

# Express the mesh in the cartesian system.
X, Y = R*np.cos(P), R*np.sin(P)

# Plot the surface.
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

# Tweak the limits and add latex math labels.
ax.set_zlim(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')

plt.show()