Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4605

Python • How to add seperate legends to the graph

$
0
0
Hi all, I am plotting a graph of three sub-plots. I am trying to add axis labels to these sub-plots. But I don't know how to add different axis labels to corresponding sub-plots. I want to add "time-sec" to all these sub-plots' x-axis. I want to add "The according value - rad/s or m/s^2" , "cm", and " rad/s" to these corresponding subplot1,2 and 3( from top to bottom). Please help.

Here is my code:

Code:

#program4g.pyimport matplotlib matplotlib.use('Pdf')   # save as pdf        import matplotlib.pyplot as plt import numpy as np # fname = input("Enter filename: ") file = open('dataWalk.txt', "r")  data = file.read().splitlines()  # split lines into an array MAXSIZE = len(data) DEBUG   = False      # For printing debug statements           time = [0]*MAXSIZE original_Ax = [0]*MAXSIZE original_Ay = [0]*MAXSIZE original_Az = [0]*MAXSIZE original_Gx = [0]*MAXSIZE original_Gy = [0]*MAXSIZE original_Gz = [0]*MAXSIZE original_distance = [0]*MAXSIZE averaged_Ax =[0]*MAXSIZEaveraged_Ay =[0]*MAXSIZEaveraged_Az =[0]*MAXSIZEaveraged_Gx =[0]*MAXSIZEaveraged_Gy =[0]*MAXSIZEaveraged_Gz =[0]*MAXSIZEaveraged_distance =[0]*MAXSIZEi=0 for dat in data: values = dat.split()  # split on white space          time[i] = float(values[0])    # first item in file is time   original_Ax[i] = float(values[1])  original_Ay[i] = float(values[2])      original_Az[i] = float(values[3])  original_Gx[i] = float(values[4])    original_Gy[i] = float(values[5])  original_Gz[i] = float(values[6])       original_distance[i] = float(values[8]) averaged_Ax[i] = float(values[11])  averaged_Ay[i] = float(values[12])      averaged_Az[i] = float(values[13])  averaged_Gx[i] = float(values[14])    averaged_Gy[i] = float(values[15])  averaged_Gz[i] = float(values[16])       averaged_distance[i] = float(values[18])             if DEBUG: print (f"{i}\t{time[i]}\t{original_data[i]}\t{averaged_data[i]}") i = i + 1 fig = plt.figure()a1= fig.add_subplot(311)a2= fig.add_subplot(312)a3= fig.add_subplot(313)# get tick marks for the x axis, in 4 regions xmarks = np.linspace(time[0],time[MAXSIZE-1],10) plt.xticks(xmarks)  # plt.plot(time, original_Ax, label= "original_Ax", color ='#1f77b4') # plt.plot(time, original_Ay, label= "original_Ay", color ='#ff7f0e') # plt.plot(time, original_Az, label= "original_Az", color ='#2ca02c') # plt.plot(time, origi3nal_Gx, label= "original_Gx", color ='#d62728') # plt.plot(time, original_Gy, label= "original_Gy", color ='#9467bd') # plt.plot(time, original_Gz, label= "original_Gz", color ='#8c564b') # plt.plot(time, original_distance, label= "original_distance", color ='#e377c2') #subplot1a1.plot(time, averaged_Ax, label= "averaged_Ax", color ='#7f7f7f') a1.plot(time, averaged_Ay, label= "averaged_Ay", color ='#bcbd22') a1.plot(time, averaged_Az, label= "averaged_Az", color ='#17becf') a1.plot(time, averaged_Gx, label= "averaged_Gx", color ='r') a1.plot(time, averaged_Gy, label= "averaged_Gy", color ='g') #subplot2a2.plot(time, averaged_distance, label= "averaged_distance", color ='black') #subplot3a3.plot(time, averaged_Gz, label= "averaged_Gz", color ='b') a1.grid()             #show the grid a2.grid()             #show the grid  a3.grid()             #show the grida1.legend(['averaged_Ax','averaged_Ay','averaged_Az','averaged_Gx','averaged_Gy'],loc=1)a2.legend(['averaged_distance'],loc=1)a3.legend(['averaged_Gz'],loc=1)plt.savefig("plotWalk.png")
Here is a archive the data:
0.08-0.74-0.589.36-2.72-2.200.2124.291209.900.07342.99-0.74-0.589.36-2.72-2.200.2124.291209.900.07342.99
0.26-1.00-0.329.30-3.202.18-2.8324.251210.000.07343.00-0.87-0.459.33-2.96-0.01-1.3124.271209.950.07342.99
0.44-0.67-0.389.420.360.145.1124.291210.000.07342.99-0.80-0.439.36-1.850.040.8324.281209.970.07342.99

Statistics: Posted by ricky2777 — Tue Feb 11, 2025 4:27 am — Replies 0 — Views 26



Viewing all articles
Browse latest Browse all 4605

Trending Articles