x
in the following has the value:
[mpf('0.0') mpf('0.10000000000000001') mpf('0.20000000000000001')
mpf('0.30000000000000004') mpf('0.40000000000000002') mpf('0.5')
mpf('0.60000000000000009') mpf('0.70000000000000007')
mpf('0.80000000000000004') mpf('0.90000000000000002')]
import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt
x = mp.arange(0,1,0.1)
y=x
plt.plot(x,y)
plt.show()
Everything is fine
import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt
x = mp.arange(0,1,0.1)
y = 2.*x
plt.plot(x,y)
plt.show()
error occurs, says: can't multiply sequence by non-int of type 'float'. So in code 3 I change 2. to 2
import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt
x = mp.arange(0,1,0.1)
y = 2*x
plt.plot(x,y)
plt.show()
It says this time: x and y must have same first dimension.
Finally, I found I can use np.array
to make x to be an array, all the trouble gone.
import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt
x = mp.arange(0,1,0.1)
y = 2.*np.array(x)
plt.plot(x,y)
plt.show()
Can anyone explain to me, what does x
represents, what is mpf. why the above codes behave like that? If x is not an numerical array, why can it be used to plot? If it is an array, why can't it multiply by a number? I am so confused!
Your x
just a list:
>>> x = mp.arange(0, 1, 0.1)
>>> type(x)
list
That means you get the normal list behavior:
>>> x * 2.0
TypeError: can't multiply sequence by non-int of type 'float'
>>> y = [e * 2.0 for e in x]
This converts to a NumPy array of objects:
>>> np.array(x).dtype
dtype('O')
This means you can do element-wise operations:
>>> np.array(x) * 2000.0
array([mpf('0.0'), mpf('200.0'), mpf('400.0'), mpf('600.00000000000011'),
mpf('800.0'), mpf('1000.0'), mpf('1200.0000000000002'),
mpf('1400.0000000000002'), mpf('1600.0'), mpf('1800.0')], d
plt.plot(x,y)
can use lists or NumPy arrays as input.