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?

5
  • in that tutorial anaconda platform. I guess it doesn't require print. For me, if I don't add print anything is printed in the console. Commented Apr 21, 2020 at 13:10
  • check my edit. You also need to add plt.show() Commented Apr 21, 2020 at 13:15
  • Does this answer your question? Pycharm does not show plot Commented Apr 21, 2020 at 13:18
  • @Georgy The accepted answer plt.show() it doesn't help me. Commented Apr 21, 2020 at 16:50
  • @masiboo Have you tried other answers as well? Commented Apr 21, 2020 at 17:54

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Now I got this: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()
I can't use %matplotlib inline in PyCharm. how do I do it in Pycharm?
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.

3 Comments

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.
You should add labels in plt.plot(x, y1) part, within the parantheses, like: plt.plot(x, y1, label='something')
I wrote the second paragrahp just as an example, you don't have to use them to plot the graph. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.