Как нарисовать несколько графиков в одну фигуру в matplotlib? - PullRequest
0 голосов
/ 08 мая 2019

У меня есть 16 файлов .xlsx (a1.xlsx… a16.xlsx), каждый из которых содержит 54 столбца и n строк. Я хотел бы выбрать те же два столбца в каждом файле, чтобы построить эти пары как график рассеяния и реализовать простую линейную регрессию для этих пар. И я хотел бы нарисовать эти отдельные 16 графиков (plot1-X1vsY1, Plot2-X2vsY2,…, plot16-X16vsY16) на том же рисунке, что и 4 x 4 вспомогательных участка.

У меня есть скрипт на python, который может дать график для одного файла одновременно (Сценарий 1). Кроме того, у меня есть другой сценарий, использующий размещение имен файлов в списке в текущей папке (сценарий 2). Как объединить эти сценарии, чтобы отобразить все файлы на одной фигуре?

Сценарий 1:

# Importing modules
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
#
def my_plotter(ax, data1, data2):
    """
    A helper function to make a scatter graph

    Parameters
    ----------
    ax : Axes
        The axes to draw to
    data1 : array
       The x data
    data2 : array
       The y data

    Returns
    -------
    out : list
        list of artists added
    """
    stdev = data2.std()
    out = ax.errorbar(data1, data2, yerr=stdev, ls='', marker='o',
             markerfacecolor='w', markeredgecolor='k', capsize=0.5, capthick=0, ecolor='darkgray')
    ax.set_xlim(xmin=0, xmax=220)
    ax.set_ylim(ymin=0, ymax= 0.16)
    return out
#
def my_fit(ax, data1, data2):
    """
    A helper function to fit a linear model which is formed y = m*x + n

    Parameters
    ----------
    ax : Axes
        The axes to draw to
    data1 : array
       The x data
    data2 : array
       The y data

    Returns
    -------
    out : list
        list of artists added
    """
    # Firstly, return a copy of the array collapsed into one dimension.
    data1 = data1.flatten()
    data2 = data2.flatten()
## Using scipy to be able to conduct a regression analysis on two arrays (x and y)
## in order to determine the intercept and slope, along with the p-value, 
## R-Squared value, and standard error.
    slope, intercept, r_value, p_value, std_err = stats.linregress(data1,data2)
    line = slope * data1 + intercept
    out = ax.plot(data1, line, c=(0, 0, 0), lw=2, label= r'$\kappa(r)$' ' = {:.3E} x $r$ + {:.3f}'.format(slope,intercept))
    plt.xlim(0, 220)
    plt.ylim(0, 0.16)
    return out
#
fig, ax = plt.subplots(nrows=1, ncols=4, sharex=True, sharey=True, squeeze=False, figsize=(16,4))

my_plotter(ax[0,0], x, y1)
my_fit(ax[0,0], x, y1)
my_plotter(ax[0,1], x, y2)
my_fit(ax[0,1], x, y2)
my_plotter(ax[0,2], x, y3)
my_fit(ax[0,2], x, y3)
my_plotter(ax[0,3], x, y4)
my_fit(ax[0,3], x, y4)

for i, ax in enumerate(ax.flat):
...

Сценарий 2:

for f in sorted(glob.glob('*.xlsx')):
   df = pd.read_excel(f, sheetname=1)
# Select variables for drawing scattering plots
   x = df[['epi.(km)']].values
   y = df[['kh']].values
   plt.scatter(x,y)
...
   fig, axes = plt.subplots(4,4, figsize=(10,6), sharey=True, dpi=120)
# Plot each axes
   for i, ax in enumerate(axes.ravel()):
       ax.scatter(x, y)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...