user3848207
user3848207

Reputation: 4917

matplotlib Plot does not appear

I am using python v3.6 with pycharm and anaconda. I tried to run the code below to plot a simple sine wave;

import numpy as np
import matplotlib.pyplot as plt

# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
# Create a second array using sine
y = np.sin(x)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")
pass

The code runs smoothly without error but no plot appears. How do I get the plot to appear?

Upvotes: 0

Views: 3782

Answers (1)

ODiogoSilva
ODiogoSilva

Reputation: 2414

You're missing plt.show() at the end.

import numpy as np
import matplotlib.pyplot as plt

# Generate a sequence of numbers from -10 to 10 with 100 steps in between
x = np.linspace(-10, 10, 100)
# Create a second array using sine
y = np.sin(x)
# The plot function makes a line chart of one array against another
plt.plot(x, y, marker="x")
plt.show()
pass

or, if you want to save it into a file

plt.savefig("my_file.png")

Upvotes: 4

Related Questions