温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python绘图常用知识有哪些

发布时间:2021-07-08 17:54:40 来源:亿速云 阅读:273 作者:chen 栏目:大数据

这篇文章主要介绍“python绘图常用知识有哪些”,在日常操作中,相信很多人在python绘图常用知识有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python绘图常用知识有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

python绘图常用知识

figure()、subplot()和subplots()、figure.add_subplot()、figure.add_axes()、plt.plot()、plt.xlim()和plt.ylim()、plt.legeng()、plt.show()

figure()函数

函数原型:

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

-- Create a new figure, or activate an existing figure. 新建一个画布或者激活一个已经存在的画布

参数:

  • num: int or str, figure的唯一编号。不指定调用figure时就会默认从1增加自动传入int。

  • figsize: 指定figure的宽和高,单位为英寸;=(weight,height)

  • dpi: 指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张

  • facecolor: 背景颜色 ,facecolor='blue'

  • edgecolor: 边框颜色

  • frameon: 是否显示边框

(1)对于只画一张图,可以不使用plt.figure(),会默认将图形画在一张画布中

import matplotlib.pyplot as plt
x=[1,2,3]
y1=[1,2,4]
y2=[1,4,8]
plt.plot(x,y1,color = "red",label = "red")
plt.plot(x,y2,color = "green",label = "green")
plt.legend()  #无此语句会不显示右上角label
plt.show()

python绘图常用知识有哪些

(2)如果想画两张图则必须使用plt.figure()两次,可以指定num,也可以不指定num,调用两次它会默认加1

import matplotlib.pyplot as plt
x=[1,2,3]
y1=[1,2,4]
y2=[1,4,8]

plt.figure()
plt.plot(x,y1,color = "red",label = "red")
plt.legend()  #无此语句会不显示右下角label

plt.figure()
plt.plot(x,y2,color = "green",label = "green")
plt.legend()  #无此语句会不显示右上角label
plt.show()

python绘图常用知识有哪些

当然你也可以指定同一个num,他会和上面的输出结果一致,在同一张画布中输出图像

这个也就提供了我们后续向某个画布中添加图像的机会,只需要调用plt.figure(num),然后调用绘制图形的函数即可,比如plt.plot()等。

subplot()和subplots()

subplot()可以将figure 划分为n个子图,然后每次执行一次subplot()会生成一个对应位置的子图

subplot()函数原型:

subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)`

参数:

  • nrows: subplot的行数

  • ncols: subplot的列数

  • sharex :所有subplot应该使用相同的X轴刻度(调节xlim将会影响所有的subplot)

  • sharey: 所有subplot应该使用相同的Y轴刻度(调节ylim将会影响所有的subplot)

  • subplot_kw: 用于创建各subplot的关键字字典

  • **fig_kw: 创建figure时的其他关键字

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#作图1
plt.subplot(2,2,1)  #等效于plt.subplot(221)
plt.plot(x, x)
#作图2
plt.subplot(2,2,2)
plt.plot(x, -x)
#作图3
plt.subplot(2,2,3)
plt.plot(x, x ** 2)
plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
#plt.subplot(224)
#plt.plot(x, np.log(x))
plt.show()

python绘图常用知识有哪些

subplots()函数与subplot()参数相似

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 100)
#划分子图
fig,axes=plt.subplots(2,2)   #返回一个画布和画轴对象,可以取出对于的画轴然后调用绘图函数
ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]

#作图1
ax1.plot(x, x)
#作图2
ax2.plot(x, -x)
#作图3
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作图4
#ax4.plot(x, np.log(x))
plt.show()

python绘图常用知识有哪些

和subplot()不同的是,subplots()一次画出所有的画轴,然后再调用绘图函数

figure对象可以添加子图或绘图区域

figure.add_subplot()

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#新建figure对象
fig=plt.figure()
#新建子图1
ax1=fig.add_subplot(2,2,1)
ax1.plot(x, x)
#新建子图3
ax3=fig.add_subplot(2,2,3)
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#新建子图4
ax4=fig.add_subplot(2,2,4)
ax4.plot(x, np.log(x))
plt.show()

python绘图常用知识有哪些

可以看出和subplot的效果没什么不同

figure.add_axes()

import numpy as np
import matplotlib.pyplot as plt

#新建figure
fig = plt.figure()
# 定义数据

x = np.array([1, 2, 3, 4, 5, 6, 7])

#新建区域ax1

#figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%
#图像还是要在figure内部

left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
# 获得绘制的句柄
ax1 = fig.add_axes([left, bottom, width, height])  #获取一个区域
ax1.plot(x,x**2 , 'r')
ax1.set_title('a1')
#plt.plot(x, y, 'r')

#新增区域ax2,嵌套在ax1内
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
# 获得绘制的句柄
ax2 = fig.add_axes([left, bottom, width, height])#获取另一个区域
ax2.plot(x,x**3, 'b')
ax2.set_title('a2')
plt.show()

python绘图常用知识有哪些

这样就可以在figure的任何地方绘制图像,更加灵活!

plt.plot()

参数:

  • x, y : x是可选的,如果x没有,将默认是从0到n-1,也就是y的索引。

  • fmt : str, optional, 定义线条的颜色和样式的操作,如“ro”就是红色的圆圈, "r--"就是红色虚线,这是一个快速设置样式的方法,更多的参数可以参考最后一个keyboard arguments。

  • kwargs : Line2D properties, optional 这是一大堆可选内容,可以来里面指定很多内容,如“label”指定线条的标签,“linewidth”指定线条的宽度,等等

import matplotlib.pyplot as plt

a = [1, 2, 3, 4] # y 是 a的值,x是各个元素的索引
b = [5, 6, 7, 8]

plt.plot(a, b, 'g--', label = 'aa')
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title('this is a demo')
plt.legend() # 将样例显示出来
plt.show()

python绘图常用知识有哪些

plt.xlim()和plt.ylim()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1,10,1000)
y = np.random.rand(1000)

plt.scatter(x,y,label="scatter figure")
plt.legend()

plt.xlim(1,10) #指定x和y的刻度值
plt.ylim(0,1)
plt.show()

python绘图常用知识有哪些

plt.legeng()

legend--------(翻译:(地图或图标)图例、说明、解释)

1.设置图列位置

plt.legend(loc='')#  填入位置,如:upper left,则图例在左上角

2.设置图例字体大小

fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}

3.设置图例边框及背景

plt.legend(loc='best',frameon=False) #去掉图例边框
plt.legend(loc='best',edgecolor='red') #设置图例边框颜色
plt.legend(loc='best',facecolor='blue') #设置图例背景颜色,若无边框,参数无效

4.设置图例标题

legend = plt.legend(["BJ", "SH"], title='Beijing VS Shanghai')
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,10,1)
plt.plot(x,x,'r--',x,np.cos(x),'g--',marker='*')
plt.xlabel('row')
plt.ylabel('cow')
plt.legend(["BJ","SH"],loc='upper left',title='Beijing VS Shanghai'
         # ,frameon=False
          ,edgecolor='green'
          ,facecolor='black')
plt.show()

python绘图常用知识有哪些

plt.show()

plt.show()是显示图片,对于有人说为什么我没有使用此函数,一样可以显示图片??

那可能是你在使用IPython这一类的交互式编程环境,比如jupyter notebook 也属于内部就是调用了IPython,因为这一类可以及时输出结果,如果你是要IDE(集成开发环境,如pycharm),就必须使用plt.show()

到此,关于“python绘图常用知识有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI