新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:如何使用python繪制折線圖?
使用python繪制折線圖過程

創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元資陽做網(wǎng)站,已為上家服務(wù),為資陽各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
1、導(dǎo)入庫和設(shè)置輸入折線圖數(shù)據(jù)
import numpy as np import matplotlib.pyplot as plt # x軸刻度標(biāo)簽 x_ticks = ['a', 'b', 'c', 'd', 'e', 'f'] # x軸范圍(0, 1, ..., len(x_ticks)-1) x = np.arange(len(x_ticks)) # 第1條折線數(shù)據(jù) y1 = [5, 3, 2, 4, 1, 6] # 第2條折線數(shù)據(jù) y2 = [3, 1, 6, 5, 2, 4]
2、設(shè)置畫布大小并繪制折線
plt.figure(figsize=(10, 6)) # 畫第1條折線,參數(shù)看名字就懂,還可以自定義數(shù)據(jù)點(diǎn)樣式等等。 plt.plot(x, y1, color='#FF0000', label='label1', linewidth=3.0) # 畫第2條折線 plt.plot(x, y2, color='#00FF00', label='label2', linewidth=3.0) # 給第1條折線數(shù)據(jù)點(diǎn)加上數(shù)值,前兩個(gè)參數(shù)是坐標(biāo),第三個(gè)是數(shù)值,ha和va分別是水平和垂直位置(數(shù)據(jù)點(diǎn)相對數(shù)值)。 for a, b in zip(x, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 給第2條折線數(shù)據(jù)點(diǎn)加上數(shù)值 for a, b in zip(x, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 畫水平橫線,參數(shù)分別表示在y=3,x=0~len(x)-1處畫直線。 plt.hlines(3, 0, len(x)-1, colors = "#000000", linestyles = "dashed")
3、添加x軸和y軸刻度標(biāo)簽
plt.xticks([r for r in x], x_ticks, fontsize=18, rotation=20) plt.yticks(fontsize=18) # 添加x軸和y軸標(biāo)簽 plt.xlabel(u'x_label', fontsize=18) plt.ylabel(u'y_label', fontsize=18)
4、繪制折線圖標(biāo)題和圖例
# 標(biāo)題 plt.title(u'Title', fontsize=18) # 圖例 plt.legend(fontsize=18)
5、保存完成
# 保存圖片
plt.savefig('./figure.pdf', bbox_inches='tight')
# 顯示圖片
plt.show() 當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Python教程:如何使用python繪制折線圖?
網(wǎng)站路徑:http://www.dlmjj.cn/article/dhsegod.html


咨詢
建站咨詢
