Home > Software engineering >  split different plots in different figures
split different plots in different figures

Time:06-08

I would like to get different plot, but getting one figure with everything plots. Why? For example I have two script for two plot, but python merge them in one image.

import matplotlib.pyplot as plt
#% matplotlib inline
import seaborn as sns
# Use plot styling from seaborn.
sns.set(style='darkgrid')
# Increase the plot size and font size.
sns.set(font_scale=1.5)
plt.rcParams["figure.figsize"] = (12,6)
# Plot the learning curve.
plt.plot(df_stats['Training Loss'], 'b-o', label="Training")
plt.plot(df_stats['Valid. Loss'], 'g-o', label="Validation")
# Label the plot.
plt.title("Training & Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
plt.show()

# Plot the learning curve.
plt.plot(df_stats['Train acc'], 'b-o', label="Training")
plt.plot(df_stats['Valid. Accur'], 'g-o', label="Validation")
# Label the plot.
plt.title("Training & Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("accuracy")
plt.legend()
plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
plt.show()

How can I do?

CodePudding user response:

Everytime you want to create a new plot, you have to write a plt.figure(). It will open a new empty plot that you'll be able to open with several others.

CodePudding user response:

It's probably neater to use individual plot objects for each port like so:

import matplotlib as plt
import numpy as np

# create dummy data
xvals = np.linspace(0,2*np.pi, 100)
yvals = np.sin(xvals)

# make first plot object
fig1, ax1 = plt.subplots()
ax1.plot(xvals, yvals, c='m')
# make second plot object
fig2, ax2 = plt.subplots()
ax2.plot(xvals, yvals**2, c='k')

or alternatively a single, dual panel plot:

# single plot object with two panes
fig, ax = plt.subplots(1,2, figsize=(8,5))
# first panel
ax[0].plot(xvals, yvals, c='m')
# second panel
ax[1].plot(xvals, yvals**2, c='k')

Dual-panel figure

  • Related