1

I am new in Python. I am trying to learn by watching youtube videos or other online tutorials. enter image description here

When I did a similar code in Pycharm I see the following:-

import matplotlib.pyplot as plt
import pandas as pd
cars = pd.read_csv("./cars.csv")
cars = cars.rename( columns={'Unnamed: 0':'model'})
print("car:\n",cars)
y1 = cars['hp']
x = range(32)
print(plt.plot(x,y1))

Output:-

car:
                   model   mpg  cyl   disp   hp  ...  vs  am  gear  carb  2am
0             Mazda RX4  21.0    6  160.0  110  ...   0   1     4     4    2
1         Mazda RX4 Wag  21.0    6  160.0  110  ...   0   1     4     4    2
2            Datsun 710  22.8    4  108.0   93  ...   1   1     4     1    2
3        Hornet 4 Drive  21.4    6  258.0  110  ...   1   0     3     1    0
.....
31           Volvo 142E  21.4    4  121.0  109  ...   1   1     4     2    2
[32 rows x 13 columns]
[<matplotlib.lines.Line2D object at 0x7ff689975240>]

I trucked output from 1 to 31. My confusion why I don't see the graph as it is shown in the video. What is missing or what is wrong?

masiboo
  • 4,537
  • 9
  • 75
  • 136

2 Answers2

0

The problem is that in the second case you are printing the plt.plot. Just remove the print command and also add plt.show(). The first editor is called Jupyter Notebook where you activate the inline plotting by using %matplotlib inline.

 plt.plot(x, y1) 
 plt.show()
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • in that tutorial anaconda platform. I guess it doesn't require print in anaconda. For me, if I don't add print anything is printed in the console. – masiboo Apr 21 '20 at 13:13
  • Now I got this: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show() – masiboo Apr 21 '20 at 13:20
  • I can't use %matplotlib inline in PyCharm. how do I do it in Pycharm? – masiboo Apr 21 '20 at 13:21
0

Because you are just using Pandas in this way.

It should be like:

y1 = cars['hp']
x = range(32)
plt.plot(x,y1) # plots the data
plt.show() # shows the graph

You can also add more details such as:

plt.xlabel() # name of x line
plt.ylabel() # name of y line
plt.title()
plt.legend(loc='upper left') # place of the legend
plt.grid() # adds grid

etc.

CoderCat
  • 22
  • 4
  • I had to update code because of warning `plt.xticks() plt.xlabel("x") plt.ylabel("y") plt.title("title") plt.legend(loc='upper left') plt.grid()` this warning No handles with labels found to put in legend. No graph output. – masiboo Apr 21 '20 at 14:46
  • You should add labels in plt.plot(x, y1) part, within the parantheses, like: plt.plot(x, y1, label='something') – CoderCat Apr 21 '20 at 14:49
  • I wrote the second paragrahp just as an example, you don't have to use them to plot the graph. Sorry for the confusion. – CoderCat Apr 21 '20 at 14:51