Home > Net >  How to save different plots in the same png file
How to save different plots in the same png file

Time:12-14

I have to save two different scatter plots in the same PNG file. This is the code that I have been trying:

chartFileName = "gragh.png"

import matplotlib.pyplot as plt

x=data['Goals']
y=data['Shots pg']

slope, intercept, rvalue, pvalue, se = scipy.stats.linregress(x, y)

yHat = intercept   slope * x

plt.plot(x, y, '.')
dataPlot=plt.plot(x, yHat, '-')
plt.xlabel("Goals", fontsize=14, labelpad=15)
plt.ylabel("Shots pg", fontsize=14, labelpad=15)
plt.show
plt.savefig(chartFileName)

x=data['Possession%']
y=data['Goals']

slope, intercept, rvalue, pvalue, se = scipy.stats.linregress(x, y)

yHat = intercept   slope * x

plt.plot(x, y, '.')
plt.plot(x, yHat, '-')
plt.xlabel("Possession%", fontsize=14, labelpad=15)
plt.ylabel("Goals", fontsize=14, labelpad=15)
plt.show
plt.savefig(chartFileName)

If I try this, it is just saving the second plot and not both of them.

CodePudding user response:

subplot can help you.

chartFileName = "gragh.png"

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax1.plot(x, y, '.')
ax1.set_xlabel("Goals", fontsize=14, labelpad=15)
ax1.set_ylabel("Shots pg", fontsize=14, labelpad=15)

ax2 = fig.add_subplot(2,1,2)
ax2.plot(x, yHat, '-')
ax2.set_xlabel("Possession%", fontsize=14, labelpad=15)
ax2.set_ylabel("Goals", fontsize=14, labelpad=15)

fig.savefig(chartFileName)
  • Related