[英]how to individually color annotate and share y axis?
我想分享兩個子圖的 y 軸(y 軸標題在兩個圖之間。
我寫了這個:
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)')
拋出了這個錯誤:
AttributeError: 'numpy.ndarray' object 沒有屬性 'get_shared_y_axes'
我還需要根據我指定的顏色 class 為 plot 上的每個點着色,不知何故,對這些點的迭代不起作用,知道嗎?
我的完整代碼:
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()
編輯:在下面,您會看到我已經用我希望包含的內容生成的 plot。 共享的 y 軸應該是黃色箭頭所在的位置。 關於“個人顏色注釋,我需要根據用紅色箭頭突出顯示的 colour_class 為 plot 上的每個點着色。
我不太確定我是否完全理解有關 colors 的問題,但是您可以設置c=
關鍵字參數以將單個 colors 應用於散點(參見下面的代碼)。
關於共享 y 軸,可能有幾種解決方案:
第一個解決方案:您可以在創建圖形/軸時使用關鍵字參數sharey=True
:
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()
第二種解決方案:不使用sharey=True
,您只需將第一個子圖的 y 軸向右移動:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.