multipanel plots in python matplotlib

Veröffentlicht am: 16 September 2019
auf dem Kanal: Taylor Sparks
1,439
20

how to make 3X1, 2X1 etc multipanel plots in matplotlib. Here is the code I used
-*- coding: utf-8 -*-
"""
Created on Thu May 2 20:08:48 2019

@author: tsparks
"""
#grab libraries
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import matplotlib as mpl
mpl.rcParams['pdf.fonttype'] = 42

#grab dataset 1
filename = r'example.csv'
df = pd.read_csv(filename)
#pull data from CSV
x_data = df['x'].dropna()
y_data = df['y'].dropna()
x_error = df['x error'].dropna()
y_error = df['y error'].dropna()
#Set the limits of the plot for dataset1
xmin1=0
xmax1=16
ymin1=0
ymax1=60


#grab dataset 2
filename = r'example2.csv'
df = pd.read_csv(filename)
#pull data from CSV
x_data2 = df['x'].dropna()
y_data2 = df['y'].dropna()
x_error2 = df['x error'].dropna()
y_error2 = df['y error'].dropna()

#Set the limits of the plot for dataset1
xmin2=0
xmax2=16
ymin2=-5
ymax2=5
#Generate some nice colors
seshadri = ['#c3121e', '#0348a1', '#ffb01c', '#027608', '#0193b0', '#9c5300', '#949c01', '#7104b5']
0sangre, 1neptune, 2pumpkin, 3clover, 4denim, 5cocoa, 6cumin, 7berry

#Prepare multipanel plot
fig = plt.figure(1, figsize=(5, 5))
gs = gridspec.GridSpec(4,3)
gs.update(wspace=0.25, hspace=0.45)

#Generate first panel
xtr_subsplot = fig.add_subplot(gs[0:2,0:3])
plt.errorbar(x_data, y_data, xerr=x_error, yerr=y_error, label='data', linestyle='none', marker='o', color=seshadri[1], markerfacecolor='none', markersize=6) # plot data

plt.tick_params(direction='in',right=True, top=True)
plt.tick_params(labelbottom=True, labeltop=False, labelright=False, labelleft=True)
xticks = np.arange(0,(xmax1+1),(xmax1/4))
yticks = np.arange(0,(ymax1+1),(ymax1/3))
plt.minorticks_on()
plt.tick_params(direction='in',which='minor', length=5, bottom=True, top=True, left=True, right=True)
plt.tick_params(direction='in',which='major', length=10, bottom=True, top=True, left=True, right=True)
plt.xticks(xticks)
plt.yticks(yticks)
plt.legend()
plt.xlim(xmin1,xmax1)
plt.ylabel('y label (units)')

#generate second panel
xtr_subsplot = fig.add_subplot(gs[2:4,0:3])
plt.errorbar(x_data2, y_data2, xerr=x_error2, yerr=y_error2, label='data', linestyle='none', marker='o', color=seshadri[2], markerfacecolor='none', markersize=6) # plot data

plt.tick_params(direction='in',right=True, top=True)
plt.tick_params(labelbottom=True, labeltop=False, labelright=False, labelleft=True)
xticks = np.arange(0,(xmax2+1),(xmax2/4))
yticks = np.arange(-6,6.1,3)
plt.minorticks_on()
plt.tick_params(direction='in',which='minor', length=5, bottom=True, top=True, left=True, right=True)
plt.tick_params(direction='in',which='major', length=10, bottom=True, top=True, left=True, right=True)
plt.xticks(xticks)
plt.yticks(yticks)
plt.legend()
plt.xlim(xmin2,xmax2)
plt.xlabel('x label (units)')
plt.ylabel('y label (units)')

#Export figure
plt.savefig('multi_panel_error.pdf')
plt.savefig('multi_panel_error.png', dpi=300,bbox_inches="tight")


Auf dieser Seite können Sie das Online-Video multipanel plots in python matplotlib mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Taylor Sparks 16 September 2019 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 1,439 Mal angesehen und es wurde von 20 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!