简体   繁体   English

为什么我不断得到“x 和 y 必须具有相同的第一维,但具有形状 (100,) 和 (1, 100)”?

[英]Why do I keep on getting “x and y must have same first dimension, but have shapes (100,) and (1, 100)”?

I have been trying to create a plot with a colourbar, with a code that I have previously used, but this time I changed the equation that represents the values for the y-axis.我一直在尝试使用我以前使用过的代码创建一个带有颜色条的图,但这次我更改了表示 y 轴值的等式。 The code is below:代码如下:

import numpy as np
import matplotlib
import matplotlib as plt


θ= [0.01, 1, 2, 3, 4, 5] #values for the colourbar to use in equation in for loop
x=np.linspace[0.1, 8, 100]
y=1/(np.exp(x)+1)       #factor used in equation dependent on the x-axis values
a=(5.3)*10**4           # constant for the equation

norm = matplotlib.colors.Normalize(vmin=np.min(θ), vmax=np.max(θ))  #colourbar max and min values
c_m = matplotlib.cm.cool
s_m = matplotlib.cm.ScalarMappable(cmap='jet', norm=norm)

s_m.set_array([])

#below is the for loop that uses one value of θ at a time, represented as t in the equation


for t in θ:            
    plt.plot(x, a*y*x*[np.pi/(4*x) - (np.arctan(x*t**3)+ (t**3)/(1 + (t**6) * x**2))], color=s_m.to_rgba(t)) 

func = lambda x,pos: "{:g}".format(x *100000)
fmt = matplotlib.ticker.FuncFormatter(func)

c_bar=plt.colorbar(s_m, format=fmt)

plt.legend()
plt.xlabel('y=E/T')
plt.ylabel('$f_{ν_s}$')
c_bar.set_label(r'$ \theta \times 10^{-5}$ rads')
plt.show()

The problem with this code, is that it constantly gives me the following error message:此代码的问题在于它不断给我以下错误消息:

"x and y must have same first dimension, but have shapes (100,) and (1, 100)" “x 和 y 必须具有相同的第一维,但具有形状 (100,) 和 (1, 100)”

I have checked the equations multiple times but I still don't understand what is wrong.我已经多次检查方程,但我仍然不明白出了什么问题。 Initially I thought that changing my x-range not to have 0 as its lowest limit would do the trick, but it did not and the same message appears.最初我认为将我的 x 范围更改为不将 0 作为其最低限制可以解决问题,但事实并非如此,并且出现了相同的消息。

I have looked into questions with a similar problem ( Matplotlib: ValueError: x and y must have same first dimension ) but I'm still unable to solve this problem我已经研究过类似问题的问题( Matplotlib: ValueError: x and y must have same first dimension ),但我仍然无法解决这个问题

Several issue with your script.你的脚本有几个问题。 The one that is causing the dimensions error is that you are using square brackets in your call to plt.plot .导致尺寸错误的一个原因是您在调用plt.plot使用了方括号。 This creates a list, and multiplies it by the a*y*x term, giving a shape of [1, 100] .这将创建一个列表,并将其乘以a*y*x项,得到[1, 100]的形状。

So, change所以,改变

a*y*x*[np.pi/(4*x) - (np.arctan(x*t**3)+ (t**3)/(1 + (t**6) * x**2))]

to

a*y*x*(np.pi/(4*x) - (np.arctan(x*t**3)+ (t**3)/(1 + (t**6) * x**2)))

Note you also use square brackets when you create x using linspace when you should use x=np.linspace(0.1, 8, 100) .请注意,当您应该使用x=np.linspace(0.1, 8, 100)时,您在使用 linspace 创建x时也使用方括号。

And finally, your module imports are a little strange.最后,你的模块导入有点奇怪。 You need to have你需要有

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm

and then use cm.cool and cm.ScalarMappable然后使用cm.coolcm.ScalarMappable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) Matplotlib 错误:x 和 y 必须具有相同的第一维,但具有形状 (100,) 和 (449,) - Matplotlib Error: x and y must have same first dimension, but have shapes (100,) and (449,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes x和y必须具有相同的第一尺寸,但形状为(30,)和(1,) - x and y must have same first dimension, but have shapes (30,) and (1,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 我收到此错误:“fx 和 y 必须具有相同的第一维但具有 python 形状” - I got this error: "f x and y must have same first dimension but have shapes python" 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) 错误:x 和 y 必须具有相同的第一维。 为什么? - Error: x and y must have same first dimension. Why? ValueError:x和y必须具有相同的第一尺寸,但形状为(4200,)和(16800,1) - ValueError: x and y must have same first dimension, but have shapes (4200,) and (16800, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM