美文网首页
Matplotlib-PyplotTutorial(下)

Matplotlib-PyplotTutorial(下)

作者: Vagitus | 来源:发表于2018-02-19 21:36 被阅读0次

控制线条的属性

  • linewidth 线宽
  • dash style 折角样式
  • antialiased 反锯齿
  • ...
line, = plt.plot([1,2,3], [2,3,7],'-', linewidth=2.0)
line.set_antialiased(False)  # 关闭反锯齿
plt.show()
result:

设置线条属性:setp()

lines = plt.plot([1,2,3], [2,3,7])
# 关键字参数
plt.setp(lines, color='r', linewidth=2.0)
# 或 MATLAB 样式的 '键值对'
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)
plt.show()
result:

可设置的所有属性:

properties values
alpha float
animated [True | False]
antialiased or aa [True | False]
clip_box a matplotlib.transform.Bbox instance
clip_on [True | False]
clip_path a Path instance and a Transform instance, a Patch
color or c any matplotlib color
contains the hit testing function
dash_capstyle ['butt' | 'round' | 'projecting']
dash_joinstyle ['miter' | 'round' | 'bevel']
dashes sequence of on/off ink in points
data (np.array xdata, np.array ydata)
figure a matplotlib.figure.Figure instance
label any string
linestyle or ls [ '-' | '--' | '-.' | ':' | 'steps' | …]
linewidth or lw float value in points
lod [True | False]
marker [ '+' | ',' | '.' | '1' | '2' | '3' | '4' ]
markeredgecolor or mec any matplotlib color
markeredgewidth or mew float value in points
markerfacecolor or mfc any matplotlib color
markersize or ms float
markevery [ None | integer | (startind, stride) ]
picker used in interactive line selection
pickradius the line pick selection radius
solid_capstyle ['butt' | 'round' | 'projecting']
solid_joinstyle ['miter' | 'round' | 'bevel']
transform a matplotlib.transforms.Transform instance
visible [True | False]
xdata np.array
ydata np.array
zorder any number

获取参数列表:

plt.setp(lines)

多个图像区域(figures)和轴(axes)下作图

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
result:

figure()函数是可选的,因为 figure(1)会默认被执行
如果不指定 subplot(), subplot(111)也是默认的

subplot()包括 行号,列号,图号,如果 行号*列号<10
三个参数可以写在一个参数里
所以 subplot(111)就相当于subplot(1,1,1)

plt.figure(1) 
###
plt.subplot(211)         
plt.plot([1, 2, 3])

plt.subplot(212)           
plt.plot([4, 5, 6])

plt.figure(2)  
###             
plt.plot([4, 5, 6])        

plt.figure(1)
###      
plt.subplot(211)
plt.title('Easy as 1, 2, 3')

plt.show()

多个figures的情况下,可以通过figure()选择不同的figure
通过subplot()选择不同的plot

result:

cla() 清除当前plot
clf()清除当前figure

text()

plt.plot([1,5],[1,5])
plt.text(3,2.5,'y=x')
plt.show()
result:

xlabel(), ylabel() and title()分别在固定的地方标示文本

plt.plot([1,5],[1,5],'b')
t = plt.text(3,2.5,'y=x')
plt.setp(t,'color','red')
plt.show()

result:


文本也可以使用 setp() 来改变其属性

文本中使用数学表达式

plt.text(.5, .5, r'$\sigma_i=15$')
plt.show()
result:

matplotlib 内建数学表达式: Writing mathematical expressions.

annotate()

plt.annotate('(0,0)', xy=(0, 0), xytext=(1, 1),
             arrowprops=dict(facecolor='black', shrink=0.05),)
plt.ylim(0, 2)
plt.xlim(0, 2)
plt.show()
result:

产生正态分布随机数,画直线, log函数, 对称log函数,分对数函数

from matplotlib.ticker import NullFormatter  

np.random.seed(19680801)

y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)


# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)


# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)

plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.35,
                    wspace=0.35)

plt.show()
result:

欢迎关注我的博客Vagitus – Pythonista

相关文章

  • Matplotlib-PyplotTutorial(下)

    控制线条的属性 linewidth 线宽 dash style 折角样式 antialiased 反锯齿 ... ...

  • Matplotlib-PyplotTutorial(上)

    Import plot()原型matplotlib.pyplot.plot(*args, **kwargs) 1....

  • 下下҈下҈下҈下҈下҈下҈雪҈啦҈ 🧣🧣🧣

    更多的排版新样式|排版技巧|涨粉技巧|运营经验, 请关注135编辑器,或者直接关注135编辑器公众号(ID:edi...

  • 207(下)评语下

    李可欣(10): 这学期对你来说一定收获满满吧!课内学习你依然保持你一贯的认真的态度,课外也体验也也一样是宝贵的财...

  • 下洲•下伊

    记忆中,从下洲到下伊的路似乎很漫长。从下洲到下伊,爷爷走了无数次。从下伊到下洲,奶奶一走就是一辈子。 从汤溪镇往中...

  • 下猫下狗

    你的眼睛是红色系的 你的嘴唇有渐变的效果 你的睫毛浓密又纤长 你穿一条露肩的黑色连衣裙 仿佛要去参加一场晚宴 你只...

  • 下吧,下吧

    酝酿了许久的阴沉天气,终于在白昼刚扯开幕布时,细细雪花悠然飘洒起来。 还在睡梦中,手机闹铃就响个不停。每一个闹钟身...

  • 测试下测试下

    输入内容

  • 北下!北下!

    按照剧情设计,我应该是在寒冬凛冽离开的时节,才准备搭上这趟十余年前送我来的列车,但那时可能因为家中事务,我...

  • 《战国史》下·下

    文/精读猫 在政治方面,他们则主张需静无为。 不要总想着改变什么。 你越想把事情扭曲好的方向,他可能就变得越糟糕。...

网友评论

      本文标题:Matplotlib-PyplotTutorial(下)

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