[英]how to resize a scatter plot in matplotlib and add a reg line to it?
我有一個數組中的數據,想調整大小,即增加生成的圖的高度並繪制回歸線,我嘗試了兩次調整大小,但要么產生了“歸因錯誤”,要么在沒有數據的情況下調整了圖的大小被印在上面。
這是我的代碼:
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
data = np.array([
[22.8, 13.2],
[19.6, 4.9],
[0.3, -16.5],
[8.9, 3.9],
[13.7, 9.5],
[14.7, -0.4],
[1.9, 4.8],
[-1.8, -11.4],
[-3, -4.6],
[-5.9, 2.2],
[-13.4, -6.3],
[-5.7, -1.7],
[-6.8, 5],
])
custom_annotations = ["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"]
for i, point in enumerate(data):
plt.figure(figsize=(10,10))
plt.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
plt.annotate(custom_annotations[i], (data[i,0], data[i,1]))
plt.xlabel(r'$\Delta q$', fontsize=12)
plt.ylabel(r'$\Delta V_{0.5}$ Apo wild-type mHCN2 (mV)', fontsize=12)
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]))
plt.legend(ncol=3, loc=(1.04,0))
plt.show()
如果您從代碼中刪除plt.figure(figsize=(10,10))
,它將產生:
我感興趣的尺寸是:
或者
更新:關於回歸線
我試過了
x = np.array[:,0]
y = np.array[:,1]
m, b = np.polyfit(x, y, 1)
plt.plot(x, m*x + b)
它拋出了這個錯誤:
TypeError: 'builtin_function_or_method' object is not subscriptable
您需要添加
#regression line
x = np.array(range(-20,20))
y = np.array(range(-20,20))
m, b = np.polyfit(x, y, 1)
plt.plot(x, m*x + b)
plt.rcParams["figure.figsize"] = (12, 12) # added code
並在下面更新
fig, ax = plt.subplots()
for i, point in enumerate(data):
ax.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
ax.annotate(custom_annotations[i], (data[i,0], data[i,1]),fontsize=30)
新代碼
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
data = np.array([
[22.8, 13.2],
[19.6, 4.9],
[0.3, -16.5],
[8.9, 3.9],
[13.7, 9.5],
[14.7, -0.4],
[1.9, 4.8],
[-1.8, -11.4],
[-3, -4.6],
[-5.9, 2.2],
[-13.4, -6.3],
[-5.7, -1.7],
[-6.8, 5],
])
plt.rcParams["figure.figsize"] = (12, 12) # added code
custom_annotations = ["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, ax = plt.subplots()
for i, point in enumerate(data):
ax.scatter(point[0], point[1], marker='o', label=custom_annotations[i], c=class_colours[i], edgecolors='black', linewidths=1, alpha=0.75)
ax.annotate(custom_annotations[i], (data[i,0], data[i,1]),fontsize=30)
plt.xlabel(r'$\Delta q$', fontsize=12)
plt.ylabel(r'$\Delta V_{0.5}$ Apo wild-type mHCN2 (mV)', fontsize=12)
plt.axvline(0, c=(.5, .5, .5), ls= '--')
plt.axhline(0, c=(.5, .5, .5), ls= '--')
x = np.array(range(-20,20))
y = np.array(range(-20,20))
m, b = np.polyfit(x, y, 1)
plt.plot(x, m*x + b)
# for i, txt in enumerate(custom_annotations):
# plt.annotate(txt, (data[i,0], data[i,1]))
plt.legend(ncol=3, loc=(1.04,0))
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.