Question #392   Submitted by Answiki on 02/08/2021 at 06:09:06 PM UTC

What is the simplest example with Matplotlib?

Answer   Submitted by Answiki on 02/08/2021 at 06:14:52 PM UTC

The following Python code is probably the simplest plot with Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# Prepare data y=cos(2π.x)
x = np.arange(0.0, 2.0, 0.01)
y = np.cos(2 * np.pi * x)

# Create figure and plot data
fig, ax = plt.subplots()
ax.plot(x, y)

The above code should display the following chart:


Note that you can plot directly with plt.plot(x,y), but it's a good habit to collect fig and ax for future use (displaying grid, axis label, title ...).

2 events in history
Answer by Answiki on 02/08/2021 at 06:14:52 PM

The following Python code is probably the simplest plot with Matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# Prepare data y=cos(2π.x)
x = np.arange(0.0, 2.0, 0.01)
y = np.cos(2 * np.pi * x)

# Create figure and plot data
fig, ax = plt.subplots()
ax.plot(x, y)

The above code should display the following chart:


Note that you can plot directly with plt.plot(x,y), but it's a good habit to collect fig and ax for future use (displaying grid, axis label, title ...).

Question by Answiki 02/08/2021 at 06:09:06 PM
What is the simplest example with Matplotlib?
# ID Query URL Count

Icons proudly provided by Friconix.