包含头文件:
import numpy as np
import matplotlib.pyplot as plt
直接将文件中的二维数组读进来:
data = np.loadtxt('data.txt')
画图:
plt.plot(data[:,0], data[:,1], 'ro')
plt.show()
其中r为红色,o为形状. 默认是b-.
颜色表:
Allias | Color |
'b' | blue |
'g' | green |
'r' | red |
'c' | cyan |
'm' | magenta |
'y' | yellow |
'k' | black |
'w' | white |
颜色也可以用参数color='r'或者c='r'指定.
颜色的表示可以是'#ff0000', 或者(1,0,0) 或者red.
线条样式表:
. , o v ^ < > 1 2 3 4 s(正方形) p(五边形) * h(六边形1) H(六边形2) + x D(菱形) d(瘦菱形) | _
TICKLEFT TICKRIGHT TICKUP TICKDOWN CARETLEFT CARETRIGHT CARETUP CARETDOWN None
plot参数:
aa 抗锯齿
c 颜色
ls 线条风格
lw 线条宽度
mec
mfc
ms
mew
直方图:
plt.hist([1, 2, 3, 2, 3, 4, 3])
如果不想要黑色边框,可以在参数中增加histtype='stepfilled'.
原型:
hist(x, bins=10, range=None, normed=False, cumulative=False,
bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False, **kwargs)
网格:
plt.grid()
标题:
plt.title('分布图')
要显示中文,必须from matplotlib.font_manager import FontProperties,
再font = FontProperties(fname=r'c:windowsfontsSimsun.ttc',size=14),
最后plt.title(u'分布图', fontproperties=font)
坐标轴:
标注坐标轴:
plt.xlabel('时间')
plt.ylabel('距离')
支持Latex,例如xlabel(r"$frac{x^2}{y^4}$"),只需在字符串前加r即可.
设置坐标轴范围:
plt.xlim(0.0, 10.)
plt.axis([-1, 1, 0, 10])
对数坐标轴:
ax.semilogx(x,y) #x轴为对数坐标轴
ax.semilogy(x,y) #y轴为对数坐标轴
ax.loglog(x,y) #双对数坐标轴