php是最好的语言

利用matplotlib画常见的图

#   matplotlib画廊
import matplotlib.pyplot as plt
import numpy as np
import sys
#生成数据
x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2

plt.figure()
plt.plot(x,y1,label='y1')
plt.plot(x,y2,color='red',linewidth=1.0,linestyle='--',label='y2')
#设置长度轴的限制
plt.xlim((-1,2))
plt.ylim((-2,3))
#设置x和y轴的名字
plt.xlabel('X')
plt.ylabel('Y')
#设置x和y轴的刻度
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2,-1,1,3,],['bad','really bad','good','really good',])
#图列
plt.legend(loc='best')
#展示
plt.show()

以上是折现图

import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x * 2
fig = plt.figure(1)

ax1 = fig.add_subplot(111)
ax2 = fig.add_subplot(111)

ax1.set_title('Scatter Plot')
plt.xlabel('X')
plt.ylabel('Y')

ax1.scatter(x,y,c = 'r',marker = 'o',label='x')
ax2.scatter(y,x,c = 'g',marker = 'x',label='x1')

plt.legend(loc = 'upper right')
plt.show()

以上是散点图

import matplotlib.pyplot as plt
import numpy as npy
fig1 = plt.figure(1)

oneLeft = npy.array([0.2,1.2,2.2])
twoLeft = oneLeft + 0.2

oneHeight = npy.linspace(1,5,3)
TwoHeight = oneHeight + 0.5

rects1 =plt.bar(left = oneLeft,height = oneHeight,color=('g'),label=(('no1')),width = 0.2,align="center",yerr=0.000001)
rects2 =plt.bar(left = twoLeft,height = TwoHeight,color=('r'),label=(('no2')),width = 0.2,align="center",yerr=0.000001)

plt.legend()
plt.xticks(twoLeft,('frst','second','three'))
plt.title('Pe')

plt.ylim((0,8))

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))

autolabel(rects1)
autolabel(rects2)
plt.show()

以上是条形图

from matplotlib import pyplot
import numpy as npy

oneD = npy.random.randint(1,10,100)
pyplot.hist(oneD,10)
pyplot.show()

以上是频率直方图

# x       (每一块)的比例,如果sum(x) > 1会使用sum(x)归一化
# labels  (每一块)饼图外侧显示的说明文字
# explode (每一块)离开中心距离
# startangle  起始绘制角度,默认图是从x轴正方向逆时针画起,如设定=90则从y轴正方向画起
# shadow  是否阴影
# labeldistance label绘制位置,相对于半径的比例, 如<1则绘制在饼图内侧
# autopct 控制饼图内百分比设置,可以使用format字符串或者format function
#         '%1.1f'指小数点前后位数(没有用空格补齐)
# pctdistance 类似于labeldistance,指定autopct的位置刻度
# radius  控制饼图半径
#
# 返回值:
# 如果没有设置autopct,返回(patches, texts)
# 如果设置autopct,返回(patches, texts, autotexts)
# patches -- list --matplotlib.patches.Wedge对象
# texts autotexts -- matplotlib.text.Text对象
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

def draw_pie(labels,quants):
    # make a square figure
    plt.figure(1, figsize=(6,6))
    # For China, make the piece explode a bit
    expl = [0,0.1,0,0,0,0,0,0,0,0]   #第二块即China离开圆心0.1
    # Colors used. Recycle if not enough.
    colors  = ["blue","red","coral","green","yellow","orange"]  #设置颜色(循环显示)
    # Pie Plot
    # autopct: format of "percent" string;百分数格式
    plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
    plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
    plt.show()
    # plt.savefig("pie.jpg")
    plt.close()

# quants: GDP

# labels: country name

labels   = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy']

quants   = [1,1,1,1,1,1,1,1,1,9]

draw_pie(labels,quants)

以上是饼状图


以上代码整理自网上然后修改,可以直接拿来用的,只需要改变数据即可 

作者:xTao 分类:LNMP 浏览:2443 评论:0