简体   繁体   中英

How to rotate a subplot in matplotlib freely

I am currently writing a program where I can project a hologram video on my computer screen, I had written the code below and I do not know how to specifically rotate a subplot, I had created a 3*3 subplot and I need to rotate subplot 4 by 270 clockwise, subplot 6 by 90 clockwise and subplot 8 by 180.

Second question is how to get rid of all of the axis label... So that the hologram projected will be nice and neatly....

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)

I might have found a solution, but not a perfect one, I use math instead of code to rotate the plotting, just multiple it by negative value to flip at x and y axis, I have also added a denoiser function to lower the deviation, here is the code that I use, if anyone had any idea about how to rotate a subplot freely, please enlight me.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM