matplotlib 学习总结
作者:csj
更新时间:01.09email:59888745@qq.com
说明:因内容较多,会不断更新 xxx学习总结;
回主目录:
# matplotlib 及环境配置# 数据图的组成结构,与 matplotlib 对应的名称# 常见的数据绘图类型,与绘制方法# 您可能需要以下的准备与先修知识:# Python开发环境及matplotlib工具包# Python基础语法 # Python numpy 包使用 # 一幅数据图基本上包括如下结构: # Data: 数据区,包括数据点、描绘形状 # Axis: 坐标轴,包括 X 轴、 Y 轴及其标签、刻度尺及其标签 # Title: 标题,数据图的描述 # Legend: 图例,区分图中包含的多种曲线或不同分类的数据 # 其他的还有图形文本 (Text)、注解 (Annotate)等其他描述 # 导入 matplotlib 包相关工具包 # 准备数据,numpy 数组存储 # 绘制原始曲线 # 配置标题、坐标轴、刻度、图例 # 添加文字说明、注解 # 显示、保存绘图结果
import matplotlib.pyplot as pltimport numpy as np x = np.arange(0,10,0.2) y = np.sin(x) plt.rcParams['figure.figsize']=(12,6)# x zhou lenght =12,y zhou lenght ==6 plt.plot(x,y,color='#0F5E0F',linestyle='--',marker='*',label=r'$ s=sin{x} $') ax = plt.subplot(111) #配置标题、坐标轴、刻度、图例,hide top,right border line ax.spines['right'].set_color('none') # delete the right borther line ax.spines['top'].set_color('none') #delete the top borter line #ax.xaxis.set_ticks_position('bottom') #ax.spines['bottom'].set_position(('data', 0)) #move the x zhou to 0.00 # 移动左边边框线,相当于移动 y 轴 #ax.yaxis.set_ticks_position('left') #ax.spines['left'].set_position(('data', 0)) #plt.title(r'$the \ function \ figure \ of \ cos(), \ sin() \ and \ sqrt()$', fontsize=19) plt.title(r' this is title name ',fontsize=19) plt.xlabel(r'x', fontsize=18, labelpad=12) plt.ylabel(r'y', fontsize=18, labelpad=12.5) #设置文字描述、注解 plt.text(0.8, 0.9, r'$x \in [0.0, \ 10.0]$', color='k', fontsize=15) plt.text(0.8, 0.8, r'$y \in [-1.0, \ 4.0]$', color='k', fontsize=15) #设置图例及位置 plt.legend(['cos(x)'],loc='upper right')
# 特殊点添加注解
plt.scatter([8,],[np.sqrt(8),], 50, color ='m') # 使用散点图放大当前点plt.annotate(r'$2\sqrt{2}$', xy=(8, np.sqrt(8)), xytext=(8.5, 2.2), fontsize=16, color='#090909', arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.1', color='#090909'))# 显示网格线
#plt.grid(True)plt.show()
2 常用图形
曲线图:
matplotlib.pyplot.plot(data)
x =np.arange(-5,5,0.1)y = x**2plt.plot(x,y)plt.show()灰度图:
matplotlib.pyplot.hist(data)x =[1,2,3,4,5,6,7,8]plt.hist(x,bins=16)plt.show()散点图:
# x =[1,2,3,4,5,6,7,8]# y =[1,2,3,4,5,6,7,8]matplotlib.pyplot.scatter(data)x=np.random.normal(size=100)y=np.random.normal(size=100)plt.scatter(x,y)plt.show()箱式图:
x =[1,2,3,4,5,6,7,8]plt.boxplot(x)plt.show()
remark:
ax.scatter(x_data, y_data, color='r', alpha = 0.75 )
# 柱状图
plt.bar(x,y)# 定义绘制柱状图的函数
def barplot(x_data, y_data, error_data, x_label, y_label, title): _, ax = plt.subplots() # 柱状图 ax.bar(x_data, y_data, color = '#539caf', align = 'center') # 绘制方差 # ls='none'去掉bar之间的连线 ax.errorbar(x_data, y_data, yerr = error_data, color = '#297083', ls = 'none', lw = 5) ax.set_ylabel(y_label) ax.set_xlabel(x_label) ax.set_title(title)# 绘图函数调用
barplot(x_data = mean_total_co_day.index.values , y_data = mean_total_co_day['mean'] , error_data = mean_total_co_day['std'] , x_label = 'Day of week' , y_label = 'Check outs' , title = 'Total Check Outs By Day of Week (0 = Sunday)') # 不同种类(species)鸢尾花萼片和花瓣的大小关系(分类散点子图),中文处理plt.scatter(data['sepal_length'] , data['petal_length'], color='r', alpha=0.7)plt.scatter(data['petal_width'], data['petal_width'], color='b', alpha=0.7)plt.xlabel("x")plt.ylabel("y")plt.title("萼片与花瓣的比较".decode('utf-8'))plt.legend(['萼片与花瓣长度比较'.decode('utf-8'), '萼片与花瓣宽度比较'.decode('utf-8')], loc='upper left')总结:
关联分析、数值比较:散点图、曲线图分布分析:灰度图、密度图涉及分类的分析:柱状图、箱式图 更全的参考http://matplotlib.org/api/index.html