Home > Mobile >  how to individually color annotate and share y axis?
how to individually color annotate and share y axis?

Time:04-25

i would like to share the y-axis of two subplots (with the y-axis title in between the two plots.

i wrote this:

ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')

axs.get_shared_y_axes(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

which threw this error:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shared_y_axes'

i would also need to color each point on the plot according to the color class i specified, somehow the iteration over the points is not working, any idea?

my full code:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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=(17,9))
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')

axs.get_shared_y_axes(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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]))

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)


classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]
recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()

edit: below you see the plot that i already generated with the things i wish to include. the shared y-axis should be where the yellow arrow is. and about the "individual color annotation, i need to color each point on the plot according to the the colour_class highlighted with the red arrow.

enter image description here

CodePudding user response:

I'm not really sure if I fully understood the question about colors, however you can set the c= keyword argument to apply individual colors to scatter points (see code below).

Regarding the shared y-axis, there might be a couple of solutions:

First solution: you can use the keyword argument sharey=True when creating the figure/axes:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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"]
classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]

fig, axs = plt.subplots(1,2, figsize=(17,9), sharey=True)
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax2.set_xlabel(r'$\Delta q$')
ax1.set_ylabel(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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]))

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)

recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()

enter image description here

Second solution: instead of using sharey=True, you just move to the right the y-axis of the first subplot:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches


from scipy.stats import t



data1 = np.array([
    [22.8, 22.8],
    [19.6, 0.3],
    [0.3, 3.1],
    [8.9, -1.7],
    [13.7, 4.8],
    [14.7, -0.7],
    [1.9, -2.6],
    [-1.8, -0.03],
    [-3, -5.7],
    [-5.9, -1.5],
    [-13.4, -3.9],
    [-5.7, -21.5],
    [-6.8, -7.7],
]) 

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"]
classes = ["K464E", "K472E", "R470E", "K464A", "M155E", "K472A", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
class_colours = ["r", "r", "r", "r", "r", "r", "g", "g", "b", "b", "b", "b", "b"]

fig, axs = plt.subplots(1,2, figsize=(17,9))
ax1,ax2 = axs

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


ax1.set_xlabel(r'$\Delta  V_{0.5}$  Apo wild-type mHCN2 (mV)')
ax1.yaxis.tick_right()
ax2.set_xlabel(r'$\Delta q$')
ax2.set_ylabel(r'$\Delta \psi_mem$  cAMP-bound wild-type mHCN2 (mV)')

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]))

# Defining custom 'xlim' and 'ylim' values.
custom_xlim = (-3, 3)
custom_ylim = (-25, 25)

# Setting the values for all axes.
plt.setp(axs, xlim=custom_xlim, ylim=custom_ylim)

recs = []
for i in range(0,len(class_colours)):
    recs.append(mpatches.Rectangle((0,0),1,1,fc=class_colours[i]))
plt.legend(recs,classes,loc=1)
plt.show()

enter image description here

  • Related