python绘图

来源:互联网 发布:moonlight软件 编辑:程序博客网 时间:2024/05/21 01:44
# -*- coding:gb2312 -*-from pylab import *from matplotlib.font_manager import FontPropertiesimport matplotlib.pyplot as pltfrom matplotlib.ticker import MultipleLocator, FormatStrFormatterfont = FontProperties(fname=r"C:\\WINDOWS\\Fonts\\simsun.ttc", size=14)#C:\WINDOWS\Fontsif __name__ == '__main__':    xmajorLocator   = MultipleLocator(1000) #将x主刻度标签设置为1000的倍数    xmajorFormatter = FormatStrFormatter('%d') #设置x轴标签为整型    xminorLocator   = MultipleLocator(200) #将x轴次刻度标签设置为200的倍数    ymajorLocator   = MultipleLocator(1) #将y轴主刻度标签设置为1的倍数    ymajorFormatter = FormatStrFormatter('%s') #设置y轴标签文本的格式    plt.figure(figsize=(100,6)) #创建一个绘图对象, 并设置对象的宽度和高度    plt.xlabel(u'长度', fontproperties=font)    plt.ylabel(u'站点', fontproperties=font)    plt.title(u'python绘图', fontproperties=font)    ax = plt.subplot(111) #一般都在ax中设置,不再plot中设置    #设置主刻度标签的位置,标签文本的格式    ax.xaxis.set_major_locator(xmajorLocator)    ax.xaxis.set_major_formatter(xmajorFormatter)    ax.yaxis.set_major_locator(ymajorLocator)    ax.yaxis.set_major_formatter(ymajorFormatter)    #显示刻度网格的位置    ax.xaxis.set_minor_locator(xminorLocator)    ax.xaxis.grid(True, which='minor',color = 'gray',linestyle='--') #x坐标轴的次刻度网格,为虚线    ax.yaxis.grid(True, which='major',color = 'gray') #y坐标轴的网格    #x轴的显示刻度值    x_axis = [0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000,16000,17000,18000,19000,20000,21000]    plt.xticks(x_axis,rotation=45)    #为x轴增加网格线,此为实线    for i in range(len(x_axis)-1):        l = plt.axvline(x=x_axis[i],color='gray',linewidth=0.8)    #y轴的显示刻度值    y_axis = [u'金海路',u'爱国路',u'五里',u'隆昌',u'延吉',u'新桥站',u'申江路',u'金景路',u'杨高北路',u'巨峰路',u'复兴岛',u'东陆路',u'宁国路',u'江浦公园',u'提篮桥',u'天潼路',u'曲阜路',u'龙华',u'桂林公园',u'七莘路',u'东兰陵',u'虹梅路',u'龙漕路',u'虹莘路']    y = range(0,len(y_axis))    plt.yticks(y, y_axis, rotation=0,fontproperties=font)    ax2 = ax.twinx()   #右侧y轴的定义    plt.yticks(y, y_axis, rotation=0,fontproperties=font)  #右侧y轴的设置    plt.xlim((0,21000)) #设置x轴的显示范围    plt.ylim((0,23)) #设置x轴的显示范围    #data_x,data_y为描的46个点,并根据这些点连线    data_x = [219, 1001, 1303, 1744, 2046, 2618, 2920, 3659, 3961, 4527, 4829, 5503, 5805, 6537, 6839, 7361, 7663, 8261, 8563, 9067, 9369, 9941, 10243, 10868, 11170, 11789, 12091, 12677, 12979, 13390, 13692, 14223, 14525, 14947, 15249, 15803, 16105, 16848, 17150, 17755, 18057, 18765, 19067, 19485, 19787, 20441]    data_y = [0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23]    plt.scatter(data_x, data_y, s=3, c='r', marker='x') #绘制数据点    plt.plot(data_x, data_y, linewidth=0.2) #绘制点的连线    plt.show()