美文网首页
Matplotlib基本画图元素及属性

Matplotlib基本画图元素及属性

作者: juriau | 来源:发表于2018-11-01 22:01 被阅读0次

plot()基础画图函数

  • 例子
import numpy as np
import matplotlib.pyplot as plt

a = np.arange(10)

# 如果在一个plot上输入多个x,y 会自动画多条线哦
plt.plot(a, a*1, 'b*-', a, a*2, 'r1:')

# 也可以这样,只要在一个show()之前就可以
# plt.plot(a, a*1, 'b*-')
# plt.plot( a, a*2, 'r1:')
plt.show()
  • 效果


    result

其中的可选参数:

'b*-'相当于color='b', marker='*', linestyle='-'
'r1:'相当于color='r', marker='1', linestyle=':'

Color

The following color abbreviations are supported:

=============    ===============================
character        color
=============    ===============================
``'b'``          blue
``'g'``          green
``'r'``          red
``'c'``          cyan
``'m'``          magenta
``'y'``          yellow
``'k'``          black
``'w'``          white
=============    ===============================

Marker

=============    ===============================
character        description
=============    ===============================
``'.'``          point marker
``','``          pixel marker
``'o'``          circle marker
``'v'``          triangle_down marker
``'^'``          triangle_up marker
``'<'``          triangle_left marker
``'>'``          triangle_right marker
``'1'``          tri_down marker
``'2'``          tri_up marker
``'3'``          tri_left marker
``'4'``          tri_right marker
``'s'``          square marker
``'p'``          pentagon marker
``'*'``          star marker
``'h'``          hexagon1 marker
``'H'``          hexagon2 marker
``'+'``          plus marker
``'x'``          x marker
``'D'``          diamond marker
``'d'``          thin_diamond marker
``'|'``          vline marker
``'_'``          hline marker
=============    ===============================

Line Styles

=============    ===============================
character        description
=============    ===============================
``'-'``          solid line style
``'--'``         dashed line style
``'-.'``         dash-dot line style
``':'``          dotted line style
=============    ===============================

figure()多个画布

  • 例子
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1

# 一个figure相当于一个画布
# figure 1
plt.figure()
plt.plot(x, y1)

# figure 2
y2 = x**2
plt.figure()
plt.plot(x, y2)

# figure 3,这个figure下有两个plot,所以在这个画布下画两次
plt.figure()
plt.plot(x, y1)
plt.plot(x, y2)

# 最后show()一哈
plt.show()
  • 结果


    result

参数

parameter

subplot()在一个画布上绘制多图

方式一:直接指定划分方式和位置进行绘图。
  • 例子1
import matplotlib.pyplot as plt

plt.figure()

# 绘制第一个图
plt.subplot(2, 2, 1)
plt.plot([0, 1], [0, 1])
# 绘制第二个图
plt.subplot(2, 2, 2)
plt.plot([0, 1], [0, 1])
# 绘制第三个图
plt.subplot(2, 2, 3)
plt.plot([0, 1], [0, 1])
# 绘制第四个图
plt.subplot(2, 2, 4)
plt.plot([0, 1], [0, 1])
plt.show()

  • 效果


    result1
  • 例子2
import matplotlib.pyplot as plt

plt.figure()

# 绘制第一个图
plt.subplot(2, 1, 1)
plt.plot([0, 1], [0, 1])
# 绘制第二个图
plt.subplot(2, 3, 4)
plt.plot([0, 1], [0, 1])
# 绘制第三个图
plt.subplot(2, 3, 5)
plt.plot([0, 1], [0, 1])
# 绘制第四个图
plt.subplot(2, 3, 6)
plt.plot([0, 1], [0, 1])
plt.show()

  • 效果
results2
方式二:这个方法更直接。事先先把画板分隔好。
import numpy as np
import matplotlib.pyplot as plt

data = loadmat('ex3data1.mat')

X = data['X']
y = data['y']

figure,ax=plt.subplots(10, 10, sharex=True, sharey=True, figsize=(8,8))

for r in range(10):
    for c in range(10):
        temp = np.random.randint(5000)
        img = X[temp].reshape(20,20)
        ax[r][c].matshow(img, cmap='gray_r')
        
plt.xticks([])
plt.yticks([])
plt.show()
  • 效果

subplot的绘图区域的原理是这样的:

sections

参考:
作者:SnailTyan
链接:https://www.jianshu.com/p/28a5cf67b0cc

中国大学mooc课:Python数据分析与展示
链接:https://www.icourse163.org/course/0809BIT021B-1001870002

相关文章

网友评论

      本文标题:Matplotlib基本画图元素及属性

      本文链接:https://www.haomeiwen.com/subject/xvsfxqtx.html