首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:参数U的大小为4,与箭头位置数1不匹配

ValueError:参数U的大小为4,与箭头位置数1不匹配
EN

Stack Overflow用户
提问于 2020-08-16 11:41:43
回答 3查看 3.6K关注 0票数 1

代码:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
import math as mt
from numpy import linalg as LA
from mpl_toolkits.mplot3d import Axes3D
from sklearn.datasets import fetch_olivetti_faces
%matplotlib inline

x=np.array([1,0]) # Original vector
theta = 30 * mt.pi / 180 # 30 degress in radian
A = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]]) # Rotation matrix for theta=30 degrees
B = np.array([[3,0],[0,1]]) # Stretching matrix

Ax = A @ x  # y1 is the rotated vector
Bx = B @ x  # y2 is the stretched vector

# Reshaping and storing both x and Ax in t1 to be plotted as vectors
t1 = np.concatenate([x.reshape(1,2), Ax.reshape(1,2)])
# Reshaping and storing both x and Bx in t2 to be plotted as vectors
t2 = np.concatenate([x.reshape(1,2), Bx.reshape(1,2)])

# origin point
x_pos = 0
y_pos = 0

fig, ax = plt.subplots()


# Plotting t1
ax1.quiver(x_pos, y_pos, t1[:,0], t1[:,1], color=['b', 'g'], width=0.013, angles='xy', scale_units='xy', scale=1)
ax1.set_xlabel('x', fontsize=14)
ax1.set_ylabel('y', fontsize=14)
ax1.set_xlim([-0.5,1.5])
ax1.set_ylim([-0.5,1])
ax1.set_aspect('equal')
ax1.grid(True)
ax1.set_axisbelow(True)
ax1.set_title("Rotation transform")
ax1.axhline(y=0, color='k')
ax1.axvline(x=0, color='k')
ax1.text(1, 0.1, "$\mathbf{x}$", fontsize=16)
ax1.text(0.8, 0.6, "$\mathbf{Ax}$", fontsize=16)


plt.show()

我试图在转换后用python和jupyter笔记本绘制向量。我是新来的。我的代码在使用ax1.quiver方法后给出了错误消息。

错误消息:

代码语言:javascript
复制
ValueError                                Traceback (most recent call last)
<ipython-input-20-f96b87a2142a> in <module>
     29 
     30 # Plotting t1
---> 31 ax1.quiver(x_pos, y_pos, t1, t1, color=['b', 'g'], width=0.013, angles='xy', scale_units='xy', scale=1)
     32 ax1.set_xlabel('x', fontsize=14)
     33 ax1.set_ylabel('y', fontsize=14)

~/python-project/linear-algebra/py3env/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1436     def inner(ax, *args, data=None, **kwargs):
   1437         if data is None:
-> 1438             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1439 
   1440         bound = new_sig.bind(ax, *args, **kwargs)

~/python-project/linear-algebra/py3env/lib/python3.7/site-packages/matplotlib/axes/_axes.py in quiver(self, *args, **kw)
   5019         args = self._quiver_units(args, kw)
   5020 
-> 5021         q = mquiver.Quiver(self, *args, **kw)
   5022 
   5023         self.add_collection(q, autolim=True)

~/python-project/linear-algebra/py3env/lib/python3.7/site-packages/matplotlib/quiver.py in __init__(self, ax, scale, headwidth, headlength, headaxislength, minshaft, minlength, units, scale_units, angles, width, color, pivot, *args, **kw)
    501                                              **kw)
    502         self.polykw = kw
--> 503         self.set_UVC(U, V, C)
    504         self._initialized = False
    505 

~/python-project/linear-algebra/py3env/lib/python3.7/site-packages/matplotlib/quiver.py in set_UVC(self, U, V, C)
    574         for name, var in zip(('U', 'V', 'C'), (U, V, C)):
    575             if not (var is None or var.size == self.N or var.size == 1):
--> 576                 raise ValueError(f'Argument {name} has a size {var.size}'
    577                                  f' which does not match {self.N},'
    578                                  ' the number of arrow positions')

ValueError: Argument U has a size 4 which does not match 1, the number of arrow positions

我搜索了谷歌的这种错误信息,但我没有找到任何有用的解决方案。我不明白什么信息告诉我要改正。

EN

回答 3

Stack Overflow用户

发布于 2020-11-21 20:34:31

回滚到matplotlib=3.1.1将显示您的向量图。

对于conda:

代码语言:javascript
复制
conda remove matplotlib
conda install matplotlib=3.1.1

注意:在笔记本中,您可能会看到下面的错误与您的情节(但这不会影响您的代码)。

/site-packages/matplotlib/mpl-data/stylelib/_classic_test_patch.mplstyle.中第4行的坏键"text.kerning_factor“

只需转到该文件并注释掉第4行。请参见下面的回答:

因素“

票数 1
EN

Stack Overflow用户

发布于 2020-09-02 05:10:31

因此,我不确定您是否使用了不支持t1[:,0]表示法的较旧版本或更新版本的quiver,但它所期望的是每个t1的值。

代码语言:javascript
复制
ax1.quiver(x_pos, y_pos, t1[0,0], t1[0,1], color=['b','g'], width=0.013, angles='xy', scale_units='xy', scale=1)
ax1.quiver(x_pos, y_pos, t1[1,0], t1[1,1], color=['b','g'], width=0.013, angles='xy', scale_units='xy', scale=1)

这可以通过更改quiver版本来解决。

票数 0
EN

Stack Overflow用户

发布于 2020-12-21 13:54:26

根据多古x_posy_pos必须用[] (即[x_pos, y_pos])括起来。我使用matplotlib 3.3.2,它可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63436424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档