版权声明:本文为博主原创文章,转载时请注明原作者,谢谢! https://blog.csdn.net/qq_31112205/article/details/84292747
每次使用matplotlib时,总是东一块西一块的到处找想要的功能,没有一个具体的查找思路和可靠的示例来源。同时,对matplotlib没有整体概念,就仅仅是调用部分函数,趁着再次的使用,做一个小总结,大致的理一理。
1. matplotlib的基本知识
figure对象及它包含的对象:
Axes和Axis的区别:
Axes是绘图的区域,可以理解成坐标轴上的象限,在上面可以添加线条等元素;一个figure对象可能包含一个Axes或者多个,但是每一个Axes只能属于一个figure对象。Axes类和成员函数是面向对象接口的主要入口;
Axis是Axes的边界,可以理解成坐标轴;
Artist是什么:在图上看到的元素都可以看成是Artist,比如,Figure,Axes和Axis对象,以及Text和Line2D对象。大部分的Artist都和Axes有关,Artist不能和多个Axes共有,不能从一个Axes移动到另外一个Axes上。
2.状态机环境示例
import matplotlib.pyplot as plt
import numpy as np
input_values = np.linspace(0, 25, 100)
squares1 = input_values * 2
squares2 = input_values ** 2
squares3 = input_values ** 0.5
squares4 = input_values + 2
plt.plot(input_values, squares1, lw=2, c='r', alpha=0.7, label='1')
plt.plot(input_values, squares2, lw=2, c='b', ls=':', alpha=0.7, label='2')
plt.plot(input_values, squares3, lw=2, c='g', alpha=0.7, label='3')
plt.plot(input_values, squares4, lw=2, c='b', ls='--', alpha=0.7, label='4')
#set the origin and border
ax = plt.gca() #This is object oriented
ax.spines['bottom'].set_position(('data', 5))
ax.spines['left'].set_position(('data', 5))
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
#set the interval and mark of the axis
plt.xlim((0, 25))
plt.ylim((0, 25))
xt = np.arange(0, 25, 10)
yt = np.arange(0, 25, 10)
plt.xticks(xt)
plt.yticks(yt)
# Setting the chart title and label the axes
plt.title("Title", fontsize=18)
plt.xlabel("Xlaber", fontsize=14)
plt.ylabel("Ylaber", fontsize=14)
# Setting the scale mark size
plt.tick_params(axis='both', labelsize=14)
plt.legend()
plt.savefig('EG.png', bbox_inches='tight')
plt.show()
结果:
3.面向对象接口示例
示例1:
fig = plt.figure() # an empty figure with no axes
fig.suptitle('No axes on this figure') # Add a title so we know which it is
fig, ax_lst = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
其结果如下:
2 * 2的grid的axes
示例2:
import numpy as np
import matplotlib.pyplot as plt
def autolabel(ax, rects, xpos='center'):
xpos = xpos.lower() # normalize the case of the parameter
ha = {'center': 'center', 'right': 'left', 'left': 'right'}
offset = {'center': 0.5, 'right': 0.57, 'left': 0.43} # x_txt = x + w*off
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
'{}'.format(height), ha=ha[xpos], va='bottom')
def drawbar(arr1, arr2, xlab):
ind = np.arange(len(arr1)) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width / 2, arr1, width, color='SkyBlue', label='width')
rects2 = ax.bar(ind + width / 2, arr2, width, color='IndianRed', label='high')
ax.set_ylabel('Value')
ax.set_title('Target size width and height distribution')
ax.set_xticks(ind)
ax.set_xticklabels(xlab)
ax.legend()
autolabel(ax, rects1) # autolabel(rects1, "left")
autolabel(ax, rects2) # autolabel(rects2, "right")
plt.savefig('Target size width and height distribution', bbox_inches='tight')
plt.show()
xlab = ["<50", "50-100", "101-150", "151-200", "201-250", "251-300", "301-350", ">350"]
widthval = np.arange(len(xlab)) + 3
highval = np.arange(len(xlab)) + 1
drawbar(widthval, highval, xlab)
其结果如下:
这个详情请看这里。
注: