Home > Net >  how to resize a scatter plot in matplotlib and add a reg line to it?
how to resize a scatter plot in matplotlib and add a reg line to it?

Time:06-14

i have got data in an array and would like to resize i.e. increase the height the produced plot and over plot a regression line, I made an attempt two attempts to resize but both either produced an "attribution error" or resized the plot without the data being printed on it.

this is my code:

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

if you removed plt.figure(figsize=(10,10)) from the code it will produce:

this needs to be resized/enlarged

the size I am interested in is:

wished size 1

or

wished size 2

UPDATE: regarding the regression line

I tried

x = np.array[:,0]
y = np.array[:,1]

m, b = np.polyfit(x, y, 1)

plt.plot(x, m*x   b)

and it threw this error:

TypeError: 'builtin_function_or_method' object is not subscriptable

CodePudding user response:

You need to add

plt.rcParams["figure.figsize"] = (12, 12) # added code

and update below

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)


New code

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= '--')




# 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()
  • Related