Home > database >  how to create a panel of two scatter plots?
how to create a panel of two scatter plots?

Time:04-23

I try to generate two scatterplots (side by side), and the code for that in matplotlib is quite straightforward and self-explanatory. however, my dataset is somehow complex, meaning for each x value, there are 2-3 corresponding y values, so I created an array for that.

here is the code to generate one plot:

import matplotlib.pyplot as plt
import numpy as np


plt.close('all')


data = np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 

custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]

plt.scatter(data[:,0], data[:,1], marker='o', c=data[:,1], edgecolors='black', linewidths=1, alpha=0.75)

# plt.colorbar(orientation='horizontal')
plt.xlabel(r'$\Delta q$')
plt.ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

plt.axvline(0, c=(.5, .5, .5), ls= '--')
plt.axhline(0, c=(.5, .5, .5), ls= '--')



for i, txt in enumerate(custom_annotations):
    plt.annotate(txt, (data[i,0], data[i,1]))

when i attempted to generate a panel of two, i intuitively defined one as data1 and the other as data2 and proceeded with the matplotlib code, here is how:

import matplotlib.pyplot as plt
import numpy as np



data1 = np.array([
    [-2, 22.8],
    [-2, 19.6],
    [-2, 0.3],
    [-1, 8.9],
    [-1, 13.7],
    [-1, 14.7,],
    [ 0, 1.9],
    [ 0, -1.8],
    [ 1, -3],
    [ 1, -5.9],
    [ 1, -13.4],
    [ 2, -5.7],
    [ 2, -6.8],
]) 

data2 =  np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 


custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]


ax1.scatter(data1[:,0], data1[:,1])
ax2.scatter(data2[:,0], data2[:,1])

ax1.set_title('Experimental Apo mHCN2 Channel')
ax1.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

ax2.set_title('Electrostatic Potential Calculations - Holo mHCN2 Channel')
ax2.set_xlabel(r'$\Delta q$')
ax2.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

plt.axvline(0, c=(.5, .5, .5), ls= '--')
plt.axhline(0, c=(.5, .5, .5), ls= '--')



for i, txt in enumerate(custom_annotations):
    plt.annotate(txt, (data1[i,0], data1[i,1]))
    plt.annotate(txt, (data2[i,0], data2[i,1]))


plt.show()

this generated an empty plot, so there should be something fatal that went wrong.

in addition, as the values on the y-axis differ considerably between the two datasets, i end up with a different y-axis spacing, is there a way to fix that?

CodePudding user response:

import matplotlib.pyplot as plt
import numpy as np

data1 = np.array([
    [-2, 22.8],
    [-2, 19.6],
    [-2, 0.3],
    [-1, 8.9],
    [-1, 13.7],
    [-1, 14.7,],
    [ 0, 1.9],
    [ 0, -1.8],
    [ 1, -3],
    [ 1, -5.9],
    [ 1, -13.4],
    [ 2, -5.7],
    [ 2, -6.8],
]) 

data2 =  np.array([
    [-2, 22.8],
    [-2, 0.3],
    [-2, 3.1],
    [-1, -1.7],
    [-1, 4.8],
    [-1, -0.7],
    [ 0, -2.6],
    [ 0, -0.03],
    [ 1, -5.7],
    [ 1, -1.5],
    [ 1, -3.9],
    [ 2, -21.5],
    [ 2, -7.7],
]) 

custom_annotations = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]

fig, axs = plt.subplots(1,2, figsize=(12,9), sharey='row')
ax1,ax2 = axs

ax1.scatter(data1[:,0], data1[:,1])
ax2.scatter(data2[:,0], data2[:,1])

ax1.set_title('Experimental Apo mHCN2 Channel')
ax1.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')

ax2.set_title('Electrostatic Potential Calculations - Holo mHCN2 Channel')
ax2.set_xlabel(r'$\Delta q$')

for ax in axs:
    ax.axvline(0, c=(.5, .5, .5), ls= '--')
    ax.axhline(0, c=(.5, .5, .5), ls= '--')

for i, txt in enumerate(custom_annotations):
    ax1.annotate(txt, (data1[i,0], data1[i,1]))
    ax2.annotate(txt, (data2[i,0], data2[i,1]))
    
plt.show()

enter image description here

  • Related