美文网首页
matplotlib内容

matplotlib内容

作者: hapo | 来源:发表于2017-04-08 21:57 被阅读0次

图标的基本元素

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = y = np.arange(-1, 1, 0.1)
plt.plot(x, y)
#plot a axes with the x as x axis and y as y axis.
import matplotlib.images as mplimg
img = mplimg.imread('foo.png')
#read the foo.png 
plt.title('axes') #添加标题
plt.xlabel('xaxis') #设定x轴名称
plt.ylabel('yaxis') #设定y轴名称
plt.plot(x, y, 'ro-') #or, plt.plot(x, y, color = 'r', ls = '-', marker = 'o', lw = 1, ms = 5) ls是线的形状, marker是点的形状,lw是线宽,ms是点的大小
'''
marker and linestyle
    ``'-'``             solid line style
    ``'--'``            dashed line style
    ``'-.'``            dash-dot line style
    ``':'``             dotted line style
    ``'.'``             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
'''
'''
color
    'b'         blue
    'g'         green
    'r'         red
    'c'         cyan
    'm'         magenta
    'y'         yellow
    'k'         black
    'w'         white
'''
matplotlib.png

面向对象编程

可以用

import matplotlib.pyplot.as plt
fig = plt.figure(1, figsize(9,9))

或者,也可以用

from matplotlib.figure import Figure
fig = Figure()

创建figure对象,然后可以使用

fig = Figure(figsize = (9, 9))

创建figure对象,之后可以使用

ax1 = fig.add_subplot(121)
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.8])

建立axes对象,对于axes对象,有

ax1.set_title('title')
ax1.set_label('label')
ax1.set_xlim((-1, 1))
ax1.set_ylim((-1, 1))
ax1.set_xscale('log')
ax1.set_yscale('log')

可以用以下方式读取各种对象中的元素

ax0 = fig.axes[0] #获取axes对象

ax0.spines['top'] #坐标轴的top线
ax0.spines['bottom'] #坐标轴的bottom线
ax0.spines['left'] #坐标轴的left线
ax0.spines['right'] #坐标轴的right线
#axes.spines是一个字典对象,有'top', 'bottom', 'left', 'right'四个key值

ax0.spine['top'].set_color('none') #设置top线颜色为无色
ax0.spine['bottom'].set_position(('data', 0)) #设置bottom线的位置为数据0点

xaxis = ax0.xaxis #获取x轴的对象
ax0.xaxis.set_ticks_position('bottom') #设置xaxis的ticks的位置在bottom线

去掉set就是plt里的函数对象
可以使用一些曲线对象来美化图片

设置双y轴

ax2 = ax1.twinx()#绘制兄弟轴

colorbar绘制


进阶参考代码

# object-oriented plot

from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

fig    = Figure()
canvas = FigureCanvas(fig)
ax     = fig.add_axes([0.1, 0.1, 0.8, 0.8])

from matplotlib.path import Path
import matplotlib.patches as patches

verts = [
    (0., 0.), 
    (0., 1.),
    (0.5, 1.5),
    (1., 1.),
    (1., 0.),
    (0., 0.),
    ]

codes = [Path.MOVETO,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.CLOSEPOLY,
         ]

path = Path(verts, codes)

patch = patches.PathPatch(path, facecolor='coral')
ax.add_patch(patch)
ax.set_xlim(-0.5,2)
ax.set_ylim(-0.5,2)

canvas.print_figure('demo.jpg')

相关文章

网友评论

      本文标题:matplotlib内容

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