0

I am using the following piece of code

import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline

x = [1, 2, 3, 4, 5, 6]
y = [2, 6, 5, 12, 10, 16]

data_viz = plt.plot(x, y)

.... other code 

plt.show(data_viz)

When I run this, I dont get any results. Its simply a blank - no error, nothing. I wish to display the plot which I saved as data_viz. What can I do?

I am using Jupyter Notebook or Jupyter lab or Google colab but both are getting the same issue.

I tested this code on Chatgpt and it doesnt find any error. I tried googling it but havent been able to find a relevant answer.

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
ace2131
  • 1
  • 1
  • 2
    *"I tested this code on Chatgpt"* -> please don't mention ChatGPT here, it's irrelevant… *"I tried googling"* without specifics it also not helpful. Have you tried to run the code without `%matplotlib inline` nor `plot.show()`. Jupyter shouldn't need those. – mozway Jun 07 '23 at 10:22

1 Answers1

0

You do not show a variable in matplotlib with plt.show. Just call plt.show() after you have set up all the plots and settings with matplotlib and it will show the graph.

import pandas as pd
from matplotlib import pyplot as plt
# %matplotlib inline

x = [1, 2, 3, 4, 5, 6]
y = [2, 6, 5, 12, 10, 16]

data_viz = plt.plot(x, y)

# .... other code

plt.show()

The variable data_viz (a <matplotlib.lines.Line2D object) is probably not really necessary, just plt.plot(x, y) will do.

I suggest to explore some matplotlib examples and some documentation on how to use matplotlib in `Jupyter notebook', which basically gives you the answer. Googling is a real help and indispensable when stuck in coding :).

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29