python turtle画熊猫_Python-利用Matplotlib绘图-Pandas-Numpy

本文的目的在于使用Matplotlib绘图,自己的学习过程记录下来。

需要的软件是Python。三个Python模块,分别是Matplotlib、Pandas和Numpy。

一个图片包含的部分如图所示:图的标题(Title)、横纵坐标轴(label、axis、major tick、minor tick)、图例(legend)、网格(grid)、图(折线图、散点图等等)。

新建一个.py文件,开始图片的绘制(这里的代码给的是2*2的图片绘制)。

在头部引入Python的模块。

import numpy as np #引入numpy

import pandas as pd #引入pandas

import matplotlib.pyplot as plt #引入matplotlib绘图模块

from matplotlib.pyplot import MultipleLocator #这部分是用来设置图中坐标轴的间隔

利用Pandas读取数据。可以利用pd.read_csv()、pd.read_excel()分别读取txt、xlsx格式的数据。这里使用excel存储数据。部分数据

#读取整张表

df = pd.read_excel('./sj.xlsx', 'Sheet1',index_col=None,na_values=['NA'])

#利用pandas提供的通过位置(df.iloc[])选取数据来定位数据

#iloc[,]中分号前的代表行(row)、分号后的代表列(column)。冒号(:)代表引用行或列的所有数据。

#df.iloc[0:1,0:1]表示选取前两行、前两列的数据。

x1 = df.iloc[:,0]

y1 = df.iloc[:,1]

x2 = df.iloc[:,2]

y2 = df.iloc[:,3]

x3 = df.iloc[:,4]

y3 = df.iloc[:,5]

绘制图片前首先要新建空白的图片。

fig = plt.figure(figsize=(10,10)) #这里的图片尺寸是1000*1000像素,所以不要设置的过大

ax1 = fig.add_subplot(2,2,1, adjustable='box', aspect=0.6) #新建第一个子图,adjustable和aspect用来控制子图的纵横比例

ax2 = fig.add_subplot(2,2,2, adjustable='box', aspect=0.6)

ax3 = fig.add_subplot(2,2,3, adjustable='box', aspect=0.6)

ax4 = fig.add_subplot(2,2,4, adjustable='box', aspect=0.6)

plt.tight_layout(pad=0.4, w_pad=1.5, h_pad=1.0) #这部分用来控制四个子图之间的纵横间距,去除多余的空白部分。

绘图(这部分待补充)。

ax1.plot(x1, y1, label='linear')

ax1.plot(x2, y2, label='linear')

ax1.plot(x3, y3, label='linear')

ax2.plot(x1, y1, label='linear')

ax2.plot(x2, y2, label='linear')

ax2.plot(x3, y3, label='linear')

ax3.plot(x1, y1, label='linear')

ax3.plot(x2, y2, label='linear')

ax3.plot(x3, y3, label='linear')

ax4.plot(x1, y1, label='linear')

ax4.plot(x2, y2, label='linear')

ax4.plot(x3, y3, label='linear')

坐标轴设置。

#坐标轴标题设置

ax1.set_xlabel('FRP tendon stress increment/MPa')

ax1.set_ylabel('Load/kN')

ax2.set_xlabel('FRP tendon stress increment/MPa')

ax2.set_ylabel('Load/kN')

ax3.set_xlabel('FRP tendon stress increment/MPa')

ax3.set_ylabel('Load/kN')

ax4.set_xlabel('FRP tendon stress increment/MPa')

ax4.set_ylabel('Load/kN')

#坐标轴间隔设置

x_major_locator=MultipleLocator(50)

y_major_locator=MultipleLocator(50)

ax1.xaxis.set_major_locator(x_major_locator)

ax1.yaxis.set_major_locator(y_major_locator)

ax2.xaxis.set_major_locator(x_major_locator)

ax2.yaxis.set_major_locator(y_major_locator)

ax3.xaxis.set_major_locator(x_major_locator)

ax3.yaxis.set_major_locator(y_major_locator)

ax4.xaxis.set_major_locator(x_major_locator)

ax4.yaxis.set_major_locator(y_major_locator)

ax1.set_xlim(0,200)

ax1.set_ylim(0,300)

ax2.set_xlim(0,200)

ax2.set_ylim(0,300)

ax3.set_xlim(0,200)

ax3.set_ylim(0,300)

ax4.set_xlim(0,200)

ax4.set_ylim(0,300)

绘制完成之后显示图片或者保存图片。

plt.show()

#fig.savefig('./test.eps',dpi=600,format='eps') #format可以不设置。

fig.savefig('./test.png',dpi=600,bbox_inches='tight') #bbox_inches 去除保存图片时四周多余的空白。

保存的图片尽量保存矢量格式的,方便后期修改、P图。支持的格式有.eps, .jpeg, .jpg, .pdf, .pgf, .png, .ps, .raw, .rgba, .svg, .svgz, .tif, .tiff。一般折线图的dpi设置为600,而图像的dpi设置为300。

图例legend放在图像外侧时,如果不设置图像大小等参数,保存的图往往是不完整的。这部分等遇到了再补充。

最终绘制的图。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值