본문 바로가기

Python

How to plot a graph : mathplotlib library

반응형

In matlab, it is very easy to plot a graph. However, in python, you need to know which library do I have to import before plotting the graph.


In python, mathplotlib library supports plotting a graph like matlab.


Here is an example. Enjoy!


import numpy as np
import math
import matplotlib.pyplot as plt

x = range(0, 100) # x-axis (index)
y = np.zeros(100) # y-axis (value)

for i in range(0, 100):
y[i] = math.exp(-0.5*i)

plt.plot(x, y, label='test')
plt.xlabel('index')
plt.ylabel('exponential function')

plt.legend()
plt.show()