I am currently going back to Python to do some work and I find myself replicating the following graph:
The code I used is the following:
from matplotlib import pyplot as plt
import numpy as np
x1=[-0.196,-0.103,0.103]
x2=[-0.750,1.750,1.770]
x3=[-0.877,-0.701,-0.598]
x4=[-0.201,-0.098,0.250]
y1=[0.198,0.124,0.251]
y2=[-0.849,-0.552,-0.503]
y3=[-0.120,-0.446,-0.495]
y4=[0.845,0.796,0.695]
x_c=[-0.456,-0.456,-0.441,-0.393,-0.344,-0.339,-0.228,-0.157,-0.083,0.004,0.081,0.217,0.222,0.299,0.323,0.323,0.352]
y_c=[0.540,0.381,0.617,0.207,0.138,0.830,0.062,0.978,0.040,1.019,0.098,0.928,0.217,0.343,0.777,0.400,0.597]
plt.xlim(-1,2)
plt.ylim(-1,1)
plt.xticks(np.arange(-1, 2 + 1, 1), fontsize=12)
plt.yticks(np.arange(-1, 1 + 1, 1), fontsize=12)
plt.scatter(x1,y1, marker='s', edgecolor='black', color='lightblue', s=100)
plt.scatter(x2,y2, marker='o', edgecolor='black', color='purple', s=100)
plt.scatter(x3,y3, marker='^', edgecolor='black', color='red', s=100)
plt.scatter(x4,y4, marker='v', edgecolor='black', color='lightgreen', s=100)
plt.plot(x_c,y_c)
plt.legend(['CK','DCS','TT','TT+CK'], loc='upper right', edgecolor='black')
plt.xlabel('NMDS2', fontsize=12)
plt.ylabel('NMDS1', fontsize=12)
for pos in ['right', 'top']:
plt.gca().spines[pos].set_visible(False)
for axis in ['left','bottom']:
plt.gca().spines[axis].set_linewidth(1.5)
plt.show()
plt.savefig('3.3.2.png')
When I run the code I get the following:
How can I make the series of points x_c,y_c so that it is as shown in the initial figure?
Good day,
The easiest way to do this is to use
matplotlib.patches.Ellipse
since if you want to do it by points, as in your question, you're going to have to go through the ellipse formulas and get the "x,y" pointsExample:
This creates the following graph:
Another unused option
Ellipse
is to use the functionplot
but you have to calculate the ellipse, a generic example:This draws the following graph: