1

connected to the question Plot 2D Numpy Array, I am trying to plot:

tdos = np.transpose(tdos)
#  plot
plt.plot(tdos[0], tdos[1])
plt.plot(tdos[0], -1.0*tdos[2]) # here, basically, I need the plot upside down

and this is giving error:

   plt.plot(tdos[0], -1*tdos[2])
TypeError: unsupported operand type(s) for *: 'int' and 'numpy.ndarray'

I don't understand why. Though error is clear enough.

The transposed tdos looks like:

[['-3.463' '-3.406' '-3.349' ..., '10.594' '10.651' '10.708']
 ['0.0000E+00' '0.0000E+00' '-0.2076E-29' ..., '0.2089E+02' '0.1943E+02'
  '0.2133E+02']
 ['0.0000E+00' '0.0000E+00' '-0.3384E-30' ..., '0.3886E+02' '0.3915E+02'
  '0.3670E+02']
 ['0.0000E+00' '0.0000E+00' '-0.1181E-30' ..., '0.9742E+03' '0.9753E+03'
  '0.9765E+03']
 ['0.0000E+00' '0.0000E+00' '-0.1926E-31' ..., '0.9664E+03' '0.9687E+03'
  '0.9708E+03']]
Community
  • 1
  • 1
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • what is `tdos`? Without knowing what is inside it is difficult to say. But `-1 * A` beinng `A` a numpy array is perfectly fine (or even `-A` which is shorter). – Imanol Luengo Apr 21 '16 at 15:21
  • @imaluengo: thanks for your comment. it is updated now. – BaRud Apr 21 '16 at 15:28
  • 2
    `tdos` seems to be a string array. Try adding `tdos = tdos.astype(np.float)` before the plotting. It will cast numerical strings to float values. – Imanol Luengo Apr 21 '16 at 15:31

1 Answers1

0

I'm a little confused as to why you brought up the numpy 2d array question when you seem to want to do a scatter plot with one inverted axis. If you really do want a 2d plot of your data, try imshow :

plt.imshow(tdos)

which will turn your numpy array upside down all by itself since it starts plotting automatically in the upper left corner (intended for image data).

To plot your array normally you would use :

plt.imshow(tdos, origin="low")

If you really do want a scatter plot with one axis inverted:

plt.plot(tdos[0], tdos[2])
plt.gca().invert_yaxis()

as in Reverse Y-Axis in PyPlot. To me this is a little cleaner than multiplying by -1, but that will work too.

Community
  • 1
  • 1