matplotlib指令

summary of matplotlib using.

to find more information, click here

install

1
pip install matplotlib

basic plot

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt
'''if you want to use jupyter to show the graph, add the command below'''
%matplotlib inline

plt.figure() # generate the whole figure, and configure it
plt.subplot(xyz) # plot a graph, the figure contains x rows, y columes, and now plot the zth graph
plt.plot(x,y,...) # real plot something
plt.show() # show the graph on the screen
plt.savefig(name,...) # save the figure

element of a figure

details

to find more info, use matplotplib.pyplot API

1
2
3
4
5
6
7
8
9
10
11
12
13
plt.plot(x, y1, color="blue", linewidth=1.0, linestyle="-", label="bla"...) # configure the line style
plt.ylim(-2, 10)
plt.xlim(-1,6) # set the upper and lower bound of the axis
plt.xlabel("bla")
plt.ylabel("bla") # the label of x/y axis
plt.xticks([-1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5])
plt.yticks([-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # ticks of x/y axis
plt.legend(loc ="upper left") # the legend configure of the graph, need to add label at plot call
plt.scatter([3], [0], s=50, color="#800080") # add a scatter point
plt.annotate("bla", xy=(3.3, 0), fontsize=16, xycoords='data') # add a piece of annotate at xy location
plt.text(4, -0.5, "this point very important", fontdict={'size': 12, 'color': 'green'}) # add a piece of text at (4,-0.5)
plt.suptitle("") # add title to the figure
plt.title("") # title of the axes

bar

1
plt.bar(x,height, width,*,align='center',**kwargs)

box

1
plt.boxplot()

note: the figure will plot the upper quartile $Q_3$, the lower quartile $Q_1$, the median. let $IQR=Q_3-Q_1$ be the interquartile range. Then the lower whisker (fence) is at $max(mindata, Q_1-1.5IQR)$ and the upper whisker (fence) is at $min(maxdata, Q_3+1.5IQR)$. The points below lower whisker and above the upper whisker are the so called outliers or fliers.