0

I just installed the most recent version of Anaconda (24.11.3). I'm trying to do a simple line chart using matplotlib, but am just getting the text output instead of the actual chart.

Code to render a matplotlib chart

I tried this exact same code in both Jupyter Notebook and JupyterLab (both opened through Anaconda Navigator) and am getting the same text output instead of the chart itself in both.

These are the matplotlib versions that are installed in conda.

matplotlib versions

Anyone have any idea why this is happening and how to fix it?

6
  • Never post screenshot of code or logs this is counter productive while wasting resources and preventing the community to help you efficiently. Instead copy paste and format. Commented Mar 25 at 12:08
  • @jlandercy, got it will do in the future. Thank you. Commented Mar 25 at 12:26
  • What happens if you just remove %matplotlib inline like in this very similar question stackoverflow.com/questions/79366461/…? Commented Mar 25 at 14:37
  • %matplotlib inline is the default now. It is not needed if you aren't switching anything with %matplotlib otherwise. Not sure though why it would need to be removed other than something about your installation seems flawed as it is. You shouldn't need plt.show() that it seems you checked off as solving it if you truly have a fresh working installation. Had you restarted everything before this test? And I mean everything. The entire browser and machine. Also, done a hard refresh on your brower page where the notebook is? (On a MAc in Chrome, Shift + Comand + r does it.) Commented Mar 25 at 15:46
  • To make it easy to test what should happen, I suggest using MyBinder sessions for modern Jupyter without needing to touch your own machine. One of the Jupyter developers keeps a fairly up to date offering here. For example, JupyterLab 4.3 on Binder is one of the most recent. Launch that in your browser by hitting 'launch binder'. When the session comes up, open a new notebook and run %pip install numpy matplotlib to install those two packages. Restart the kernel and run the code ... Commented Mar 25 at 16:00

1 Answer 1

1

You are missing plt.show(). Your code returns a Line2D object and that is what the line below the code block states (also mentioning the position in your computer's memory).

This code works just fine:

x = np.linspace(-10, 10, 100)
y = np.sin(x)

plt.plot(x, y, marker="x")
plt.show()

Note that you can also have interactive plots with %matplotlib notebook

Sign up to request clarification or add additional context in comments.

11 Comments

This worked. Thank you. However, the book I'm following (Introduction to Machine Learning with Python by Andreas Müller) doesn't use plt.show() but it still renders the chart. Why the difference?
As I do not have access to the book, I cannot answer this question. So you will have to post that code (and its setup) first.
You shouldn't have to explicitly call plt.show() (and I can't reproduce the problem when not using plt.show()), so there must be something else that is stopping the plot from rendering. However, if plt.show() works then there's no harm in using it.
As Matt has pointed out, your answer suffers from some misunderstandings and incompleteness. Matt has already addressed one. I'll provide more context. In modern Jupyter, you shouldn't need plt.show() as Jupyter will actively detect an open plot object being generated and display it when the cell is run. (Usually disabling this is an issue most people actually bring up these days because otherwise it just works.) Your code should work without the plt.show() and if isn't you are using outdated tech. If users are doing that, I would suggest though using...
<continued> a semi-colon after it, like plt.plot(x, y, marker="x");. This way the object Python code will be not shown and just the plot. (That is what is shown in the OP's screenshot, a Python object code like [<matplotlib.lines.Line2D at 0x7284e62a9060>].) An additional point is that you shouldn't be advising %matplotlib notebook anymore. That is not for modern Jupyter. Please use %matplotlib ipympl in both Jupyter Notebook 7+ and JupyterLab, see here. %matplotlib notebook is for older tech (Notebook v6.4 and ....
|

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.