【问题标题】:How to save different plots in the same png file如何在同一个png文件中保存不同的图
【发布时间】:2022-01-16 16:17:21
【问题描述】:

我必须在同一个 PNG 文件中保存两个不同的散点图。这是我一直在尝试的代码:

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)

如果我尝试这个,它只是保存第二个情节而不是两个情节。

【问题讨论】:

    标签: python matplotlib plot data-science png


    【解决方案1】:

    subplot可以帮到你。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 2020-06-17
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多