简体   繁体   中英

x and y must have same first dimension, but have shapes (50,) and (10,)

I want to plot a diagram with 2 arrays but it is not working. Output says:

ValueError: x and y must have same first dimension, but have shapes (50,) and (10,)

Thank u so much!!!

import numpy as np
import matplotlib.pyplot as plt

p = float(input("Introduzca probabilidad de error de bit: "))
while p < 0 or p > 1:
     p = float(input("Introduzca probabilidad de error de bit: "))
n = int(input("Introduzca número de bits: "))
while n < 0:
     n = int(input("Introduzca número de bits: "))

q = 1-p
pexito = q**n
intentos = 1/pexito
print()
print("El número medio de intentos es:",intentos)
print()
print("La probabilidad de transmitir correctamente",n,"bits es:",pexito)

listapmf = []
listacdf = []

for i in range(n):
    pmf = (p**(n-1))*q
    listapmf.append(pmf)
for j in range(n):
    cdf = 1-(p**n)
    listacdf.append(cdf)

arraycdf = np.array(listacdf)
arraypmf = np.array(listapmf) 

x1 = np.linspace(0.0, n)
x2 = np.linspace(0.0, n)

plt.subplot(2, 1, 2)
plt.plot(x1, arraypmf, '.-')
plt.title('Intentos de transmisión en base a una probabilidad de error de bit')
plt.ylabel('PMF')

plt.subplot(2, 1, 1)
plt.plot(x2, arraycdf, '.-')
plt.xlabel('Intentos')
plt.ylabel('CDF')

plt.show()

As G.Anderson stated, the problem is you are trying to plot 50 x-axis values with only 10 x-axis values. The issue starts when you define x1 and x2 . The default for np.linspace() is to split the values passed in 50 values. I'm not sure what are you expecting, but maybe this would work? (Functionally it does work 100% but not sure if it's the logic you intend to use)

Error:

x1 = np.linspace(0.0, n) <-- Split values between 0 and n in 50 values
x2 = np.linspace(0.0, n) <-- Same

Change to:

x1 = np.linspace(0.0, 1, num = n)
x2 = np.linspace(0.0, 1, num = n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM