如何在matplotlib中自由旋转子地块

2024-09-30 01:31:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在写一个程序,我可以在我的电脑屏幕上投射一个全息图视频,我写了下面的代码,我不知道如何具体旋转一个子图,我创建了一个3*3子图,我需要顺时针旋转子图4乘以270,顺时针旋转子图6乘以90,顺时针旋转子图8乘以180

第二个问题是如何摆脱所有的轴标签。。。这样投影的全息图会很漂亮,很整齐

import pandas as pd
import serial 
import numpy as np
import matplotlib.pyplot as plt

ser = serial.Serial("COM5", 115200) # define the serial port that we are communicating to and also the baud rate
plt.style.use('dark_background')    #define the black background
plt.ion()                        # tell pyplot we need live data
fig,[[ax1,ax2,ax3],[ax4,ax5,ax6],[ax7,ax8,ax9]] = plt.subplots(3,3)     # plotting a figure with 9 subplot


Xplot = []
Yplot = []
Zplot = []
blankx = []
blanky = []
fig = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]


while True:                         #always looping this sequence
    while(ser.inWaiting()==0):      #if no input from the serial, wait and do nothing
        pass
    data = ser.readline()           #obtain the input from COM 5 
    data_processed = data.decode('utf-8')  #to get rid of the unnecessary string part
    data_split = data_processed.split(",")  # split the incoming string into a list
    x = float(data_split[0])    #to obtain seperate float values for x,y,z
    y = float(data_split[1])
    z = float(data_split[2])
    reset = int(data_split[3])  # reset will output 1
    draw = int(data_split[4])   # draw will output 2

    if(draw == 2):
        Xplot.append(x)         #if draw is given instruction, add the x,y,z value into the list to be plot on the graph
        Yplot.append(y)
        Zplot.append(z)


    ax1.plot(blankx,blanky)         # subplotting
    ax2.plot(Xplot,Yplot,"ro")
    ax3.plot(blankx,blank)
    ax4.plot(Xplot,Yplot,"ro")
    ax5.plot(blankx,blank)
    ax6.plot(Xplot,Yplot,"ro")
    ax7.plot(blankx,blanky)
    ax8.plot(Xplot,Yplot,"ro")
    ax9.plot(blankx,blanky)

    if(reset == 1):
        for f in fig:       #if reset is given instruction, clear all figure and clear the elements in the plotting list
        f.clear()
        Xplot = []
        Yplot = []
        Zplot = [] 

    plt.pause(.000001)

Tags: thetoimportdataifplotserialplt
1条回答
网友
1楼 · 发布于 2024-09-30 01:31:13

我可能已经找到了一个解决方案,但不是一个完美的解决方案,我使用数学而不是代码来旋转绘图,只是将其乘以负值在x和y轴翻转,我还添加了一个去噪函数来降低偏差,这是我使用的代码,如果有人知道如何自由旋转子地块,请告诉我

import pandas as pd
import serial 
import matplotlib.pyplot as plt

ser = serial.Serial("COM5", 115200) # define the serial port that we are communicating to and also the baud rate
plt.style.use('dark_background')    #define the black background
plt.ion()                        # tell pyplot we need live data
fig,[[ax1,ax2,ax3],[ax4,ax5,ax6],[ax7,ax8,ax9]] = plt.subplots(3,3)     # plotting a figure with 9 subplot

rx = [0]
ry = [0]
rz = [0]
Xplot2 = []
Xplot4 = []
Xplot6 = []
Xplot8 = []
Zplot2 = []
Zplot4 = []
Zplot6 = []
Zplot8 = []
blankx = []
blankz = []
fig = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]

def switch(x):
    return x*-1

def denoiser(x):
    return (x[-1] +x[-2])/4

while True:                         #always looping this sequence
    while(ser.inWaiting()==0):      #if no input from the serial, wait and do nothing
        pass
    data = ser.readline()           #obtain the input from COM 5 
    data_processed = data.decode('utf-8')  #to get rid of the unnecessary string part
    data_split = data_processed.split(",")  # split the incoming string into a list
    rx.append(float(data_split[0]))    #to obtain seperate float values for x,y,z
    ry.append(float(data_split[1]))
    rz.append(float(data_split[2]))
    reset = int(data_split[3])  # reset will output 1
    draw = int(data_split[4])   # draw will output 2

    x = denoiser(rx)
    y = denoiser(ry)
    z = denoiser(rz)

    if(draw == 2):
        Xplot8.append(x)         #if draw is given instruction, add the x,y,z value into the list to be plot on the graph
        Zplot8.append(z)

        Xplot2.append(switch(x))
        Zplot2.append(switch(z))

        Xplot4.append(x)
        Zplot4.append(switch(z))

        Xplot6.append(switch(x))
         Zplot6.append(z)


    ax1.plot(blankx,blankz)         # subplotting
    ax1.axis("off")
    ax2.plot(Xplot2,Zplot2,"ro")
    ax2.axis("off")
    ax3.plot(blankx,blankz)
    ax3.axis("off")
    ax4.plot(Xplot4,Zplot4,"ro")
    ax4.axis("off")
    ax5.plot(blankx,blankz)
    ax5.axis("off")
    ax6.plot(Xplot6,Zplot6,"ro")
    ax6.axis("off")
    ax7.plot(blankx,blankz)
    ax7.axis("off")
    ax8.plot(Xplot8,Zplot8,"ro")
    ax8.axis("off")
    ax9.plot(blankx,blankz)
    ax9.axis("off")

if(reset == 1):
    for f in fig:       #if reset is given instruction, clear all figure and clear the elements in the plotting list
        f.clear()
    Xplot2 = []
    Xplot4 = []
    Xplot6 = []
    Xplot8 = []
    Zplot2 = []
    Zplot4 = []
    Zplot6 = []
    Zplot8 = [] 

plt.pause(.000001)

相关问题 更多 >

    热门问题