Python 向我的子绘图中添加图例的最简单方法是什么?

Python 向我的子绘图中添加图例的最简单方法是什么?,python,numpy,matplotlib,spyder,Python,Numpy,Matplotlib,Spyder,我是这个网站的新手,我用python编写了一些代码,在一个子图上绘制一些向量和特征向量,在另一个子图上绘制它们与矩阵的点积。最后,我想在图例中加入每个向量/特征向量的颜色,如子图所示 这是我的密码: import numpy as np import matplotlib.pyplot as plt #The Matrix A=np.array([[1,1.5],[0,2]]) #Calculating Eigenvectors of The Matrix l,e=np.linalg.eig(

我是这个网站的新手,我用python编写了一些代码,在一个子图上绘制一些向量和特征向量,在另一个子图上绘制它们与矩阵的点积。最后,我想在图例中加入每个向量/特征向量的颜色,如子图所示

这是我的密码:

import numpy as np
import matplotlib.pyplot as plt
#The Matrix
A=np.array([[1,1.5],[0,2]]) 
#Calculating Eigenvectors of The Matrix
l,e=np.linalg.eig(A)
EVect1=e[0]
EVect2=e[1]
#Combined Array of Vectors in Question and calulated Eigenvectors
V=np.array([[1,2],[0,1],[-1,2],[-0.5,1],e[0],e[1]])

#labels for vectors
Legend_labels=(['Vector1','Vector2','Vector3','Vector4''Eigenvector1','Eigenvector2'])
#List of colours to plot vectors
c=(['r','y','m','b','g','k'])

#making the subplots
for i in range(1, 3):
        plt.subplot(1, 2, i)
        plt.xlabel('x') 
        plt.ylabel('y')
        plt.xlim(-5,5)
        plt.ylim(-5,5)
        plt.grid()

def plot_vector(v,c): 
    plt.arrow(0,0,v[0],v[1],color=c,head_width=0.2) 

#making the first sub plot                       
plt.subplot(1,2,1)
plt.title("The Four Vectors and the Eigenvectors of Matrix A", fontsize=6)
for i in range(0,6):
    plot_vector(V[i],c[i])

#making the second subplot
plt.subplot(1,2,2)
plt.title("Dot Products of Each Vector with Matrix A",fontsize=6)
for i in range(0,6):
    plot_vector((np.dot(A,V[i])),c[i])  
这将生成两个子图,如图所示

抱歉,如果我在帖子中设置了错误的格式,请不要批评我。

像这样:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D    
#The Matrix
A=np.array([[1,1.5],[0,2]]) 
#Calculating Eigenvectors of The Matrix
l,e=np.linalg.eig(A)
EVect1=e[0]
EVect2=e[1]
#Combined Array of Vectors in Question and calulated Eigenvectors
V=np.array([[1,2],[0,1],[-1,2],[-0.5,1],e[0],e[1]])

#labels for vectors
Legend_labels=(['Vector1','Vector2','Vector3','Vector4''Eigenvector1','Eigenvector2'])
#List of colours to plot vectors
c=(['r','y','m','b','g','k'])
custom_lines = [Line2D([0],[0], color='r'),
                Line2D([0],[0], color='y'),
                Line2D([0],[0], color='m'),
                Line2D([0],[0], color='b'),
                Line2D([0],[0], color='g'),
                Line2D([0],[0], color='k')]
for i in range(1, 3):
#making the subplots
        plt.subplot(1, 2, i)
        plt.xlabel('x') 
        plt.ylabel('y')
        plt.xlim(-5,5)
        plt.ylim(-5,5)
        plt.grid()

def plot_vector(v,c): 
    plt.arrow(0,0,v[0],v[1],color=c,head_width=0.2) 

#making the first sub plot                       
plt.subplot(1,2,1)
plt.title("The Four Vectors and the Eigenvectors of Matrix A", fontsize=6)
for i in range(0,6):
    plot_vector(V[i],c[i])
    plt.legend(custom_lines, ['Vector1','Vector2','Vector3','Vector4','Eigenvector1','Eigenvector2'],loc=8)

#making the second subplot
plt.subplot(1,2,2)
plt.title("Dot Products of Each Vector with Matrix A",fontsize=6)
for i in range(0,6):
    plot_vector((np.dot(A,V[i])),c[i]) 
    plt.legend(custom_lines, ['Vector1','Vector2','Vector3','Vector4','Eigenvector1','Eigenvector2'],loc=8)
像这样:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D    
#The Matrix
A=np.array([[1,1.5],[0,2]]) 
#Calculating Eigenvectors of The Matrix
l,e=np.linalg.eig(A)
EVect1=e[0]
EVect2=e[1]
#Combined Array of Vectors in Question and calulated Eigenvectors
V=np.array([[1,2],[0,1],[-1,2],[-0.5,1],e[0],e[1]])

#labels for vectors
Legend_labels=(['Vector1','Vector2','Vector3','Vector4''Eigenvector1','Eigenvector2'])
#List of colours to plot vectors
c=(['r','y','m','b','g','k'])
custom_lines = [Line2D([0],[0], color='r'),
                Line2D([0],[0], color='y'),
                Line2D([0],[0], color='m'),
                Line2D([0],[0], color='b'),
                Line2D([0],[0], color='g'),
                Line2D([0],[0], color='k')]
for i in range(1, 3):
#making the subplots
        plt.subplot(1, 2, i)
        plt.xlabel('x') 
        plt.ylabel('y')
        plt.xlim(-5,5)
        plt.ylim(-5,5)
        plt.grid()

def plot_vector(v,c): 
    plt.arrow(0,0,v[0],v[1],color=c,head_width=0.2) 

#making the first sub plot                       
plt.subplot(1,2,1)
plt.title("The Four Vectors and the Eigenvectors of Matrix A", fontsize=6)
for i in range(0,6):
    plot_vector(V[i],c[i])
    plt.legend(custom_lines, ['Vector1','Vector2','Vector3','Vector4','Eigenvector1','Eigenvector2'],loc=8)

#making the second subplot
plt.subplot(1,2,2)
plt.title("Dot Products of Each Vector with Matrix A",fontsize=6)
for i in range(0,6):
    plot_vector((np.dot(A,V[i])),c[i]) 
    plt.legend(custom_lines, ['Vector1','Vector2','Vector3','Vector4','Eigenvector1','Eigenvector2'],loc=8)

你自己找到的,但我让你知道我用更少的电话做的版本,如果有帮助的话:):


您自己找到的,但如果有帮助的话,我会让您使用我正在使用的版本,使用更少的呼叫:):


你也不需要打六次电话。最后一次就足够了。而且你不需要打六次电话。最后一次就足够了。