Home > Mobile >  How to add multiple plot figures in one window with python?
How to add multiple plot figures in one window with python?

Time:12-19

Sorry for all mistakes, English is not my native language. I have a code that reads from csv file and plots data of different types and I need that plots to be in one window. How can I achieve this? This is my code:

import pandas as pd
import csv
import matplotlib.pyplot as ppl

def point_plot(num, x, y, type, title, color, save):

    ppl.figure(num)
    ppl.plot(x, y, type, color=color)
    ppl.title(title)
    ppl.grid()
    ppl.savefig(save)

def linear_plot(data_, title, save):
    
    data_.plot()
    ppl.title(title)
    ppl.grid()
    ppl.savefig(save)

f = open("sensor_data17.csv", encoding="utf-8")
csv_read = csv.reader(f, delimiter=";")

header = next(csv_read)

f.close()

data_csv = pd.read_csv("sensor_data17.csv", header=None, names=header, skiprows=1, delimiter=";")

point_plot(1, data_csv["Pose X"], data_csv["Pose Y"], "o", "Pose", "black", "A_Pose.png")
point_plot(2, list(range(1, 243)), data_csv["Dust PPM"], "o", "Dust PPM", "green", "A_Dust.png")
linear_plot(data_csv[["Pressure Pa"]], "Pressure Pa", "A_Pressure.png")
linear_plot(data_csv[["Lightning Lx", "Air quality PPM", "Temperature C","Humidity %","Altitude m","Loudness"]], "CSV Data", "A_Data.png")

ppl.show()

Appreciate any help.

CodePudding user response:

To fit NxM plots in one window you can use either

  1. subplots(N,M) or

  2. GridSpec(N,M)

Among the two, subplots is easier to use and gives an evenly divided plot space. However, if you desire to divide the plotting space in a non-even way (such as in shown image) use can use GridSpec.

enter image description here

CodePudding user response:

For the exact case with 2 figures, try ading

def point_plot(num, SUBPLOT, x, y, type, title, color, save)  # SUBPLOT

ppl.figure(num)
plt.subplot(SUBPLOT) #<--------- this whole line
ppl.plot(x, y, type, color=color)

and when calling

point_plot(1, 211, data_csv["Pose X"], data_csv["Pose Y"], "o", "Pose", "black", "A_Pose.png") # number 211

you can refer to matplotlib example

  • Related