Use plot to draw a line graph. Some examples are shown below.
Pass the x, y values separately, like plot (x, y). The line style can be specified with the parameter linestyle.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = np.linspace(-6,6,1000)
ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black', linestyle='solid')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='black', linestyle='dashed')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='black', linestyle='dashdot')
ax.set_title('First line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()
You can change the line thickness with linewidth.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = np.linspace(-6,6,1000)
ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black', linestyle='solid', linewidth = 3.0, label='line1')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='black', linestyle='dashed',linewidth = 1.0, label='line2')
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='black', linestyle='dashdot', linewidth = 0.5,label='line3')
ax.set_title('Second line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
ax.grid(True)
fig.show()
Change the line color with color.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = np.linspace(-6,6,1000)
ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='red', linestyle='solid', linewidth = 1.0)
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.5), color='green',linestyle='solid',linewidth = 1.0)
ax.plot(x,norm.pdf(x, loc=0.0, scale=0.25), color='blue', linestyle='solid', linewidth = 1.0)
ax.set_title('Third line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()
When marker is specified, marker is drawn at the data location of the line graph. When there is a lot of data, the line disappears with the marker like the line below. By specifying markevery, you can specify how many markers are drawn at intervals.
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = np.linspace(-6,6,1000)
ax.plot(x,norm.pdf(x, loc=0.0, scale=1.0), color='black', linestyle='solid', linewidth = 1.0, marker='o')
ax.plot(x,0.5 + norm.pdf(x, loc=0.0, scale=1.0), color='black', linestyle='solid', linewidth = 1.0, marker='o', markevery = 50)
ax.set_title('4th line plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
fig.show()
Typical of marker
marker | description |
---|---|
. | point |
o | circle |
v | Lower triangle |
^ | Upper triangle |
s | square |
+ | plus |
x | cross |
* | star |
http://matplotlib.org/api/markers_api.html
Recommended Posts