LoginSignup
4

More than 3 years have passed since last update.

only size-1 arrays can be converted to Python scalarsが出る原因

Last updated at Posted at 2020-05-23

問題

only size-1 arrays can be converted to Python scalars

これが出てきてうまくグラフがかけない!!

問題のソースコードはこれ

import matplotlib.pyplot as plt
import math
import np
x = np.linspace(0,1, 10000);
def y(a):
    return math.exp(a)
plt.figure(0)
plt.plot(x, y(x))
plt.show()

原因

mathの関数は行列を扱うことができない

expをnumpyの関数をつかってあげれば解決

plt.plot(x, y(x))

この部分では行列投げ込んでるから注意

よって

def y(a):
    return np.exp(a)

としたら治る

解決後のコード

import matplotlib.pyplot as plt
import math
import numpy
x = np.linspace(0,1, 10000);
def y(a):
    return np.exp(a)
plt.figure(0)
plt.plot(x, y(x))
plt.show()

参考:
https://blog.csdn.net/u013634684/article/details/49305655?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
4