python-matplotlib模塊

matplotlib

matplotlib官方文檔

什么是matplotlib

matplotlib: 最流行的Python底層繪圖庫,主要做數(shù)據(jù)可視化圖表,名字取材于MATLAB,模仿MATLAB構(gòu)建

matplotlib基本要點

# 導入pyplot
from matplotlib import pyplot as plt

# 數(shù)據(jù)在x軸的位置,是一個可迭代對象
x = range(2, 26, 2)

# 數(shù)據(jù)在y軸的位置,是一個可迭代對象
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]

# 傳入x和y,通過plot繪制出折線
plt.plot(x, y)

# 在執(zhí)行程序的時候展示圖形
plt.show()

設置圖形大小和保存圖片

from matplotlib import pyplot as plt

# 設置圖形大小
fig = plt.figure(figsize=(20, 8), dpi=80)

x = range(2, 26, 2)
y = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15]

plt.plot(x, y)

# 保存圖片,可保存svg格式
plt.savefig('./sig_size.jpg')

調(diào)整x軸和y軸的刻度

'''
a表示10點到12點中每一分鐘的氣溫,如何繪制每分鐘氣溫的變化情況?
a = [random.randint(20,35) for i in range(120)]
'''

import random
from matplotlib import pyplot as plt

fig = plt.figure(figsize=(20, 8), dpi=80)

x = range(120)

# 設置隨機種子,讓不同時候隨機的結(jié)果都一樣
random.seed(10)

y = [random.randint(20, 35) for i in range(120)]

plt.plot(x, y)

# 設置x軸的刻度
x_ticks = ['10點{}分'.format(i) for i in x if i < 60]
x_ticks += ['11點{}分'.format(i) for i in x if i > 60]

# 讓列表x和x_ticks上的數(shù)據(jù)都傳入,最終會在x軸上一一對應顯示
# 兩組數(shù)據(jù)的長度必須保持一致,否則不能完全覆蓋整個軸
# 使用列表切片,每隔5個選一個數(shù)據(jù)進行展示
# 為了讓字符串不會被覆蓋,使用rotation選項,讓字符串旋轉(zhuǎn)90度顯示
plt.xticks(x[::5], x_ticks[::5], rotation=90)

plt.show()

設置中文顯示

'''
a表示10點到12點中每一分鐘的氣溫,如何繪制每分鐘氣溫的變化情況?
a = [random.randint(20,35) for i in range(120)]
'''

import random
from matplotlib import pyplot as plt
from matplotlib import font_manager

fig = plt.figure(figsize=(20, 8), dpi=80)

# 設置中文字體
myfont = font_manager.FontProperties(fname="C:/Windows/Fonts/simhei.ttf")

x = range(120)
random.seed(10)
y = [random.randint(20, 35) for i in range(120)]

plt.plot(x, y)

x_ticks = ['10點{}分'.format(i) for i in x if i < 60]
x_ticks += ['11點{}分'.format(i) for i in x if i > 60]

# 在需要使用中文的地方,添加fontproperties屬性
plt.xticks(x[::5], x_ticks[::5], rotation=45, fontproperties=myfont)

plt.show()

給圖形設置描述信息

'''
a表示10點到12點中每一分鐘的氣溫,如何繪制每分鐘氣溫的變化情況?
a = [random.randint(20,35) for i in range(120)]
'''

import random
from matplotlib import pyplot as plt
from matplotlib import font_manager

fig = plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname="C:/Windows/Fonts/simhei.ttf")

x = range(120)
random.seed(10)
y = [random.randint(20, 35) for i in range(120)]

plt.plot(x, y)

x_ticks = ['10點{}分'.format(i) for i in x if i < 60]
x_ticks += ['11點{}分'.format(i) for i in x if i > 60]

# 設置x軸描述
plt.xlabel('時間', fontproperties=myfont)
# 設置y周描述
plt.ylabel('溫度(℃)', fontproperties=myfont)
# 設置標題
plt.title('時間-溫度變化曲線', fontproperties=myfont)

plt.xticks(x[::5], x_ticks[::5], rotation=45, fontproperties=myfont)

plt.show()

練習

假設大家在30歲的時候,根據(jù)自己的實際情況,統(tǒng)計出來了你和你同桌各自從11歲到30歲每年交的女(男)朋友的數(shù)量如列表a和b,請在一個圖中繪制出該數(shù)據(jù)的折線圖,以便比較自己和同桌20年間的差異,同時分析每年交女(男)朋友的數(shù)量走勢

a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]

b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]

要求:
y軸表示個數(shù)
x軸表示歲數(shù),比如11歲,12歲等

from matplotlib import pyplot as plt
from matplotlib import font_manager

# 設置圖形大小
fig = plt.figure(figsize=(20, 8), dpi=80)

# 設置字體
myfont = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')

# x軸數(shù)據(jù),年齡
x = range(11, 31)

# y軸數(shù)據(jù),談女朋友的個數(shù)
y_a = [1, 0, 1, 1, 2, 4, 3, 2, 3, 4, 4, 5, 6, 5, 4, 3, 3, 1, 1, 1]
y_b = [1, 0, 3, 1, 2, 2, 3, 3, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]

# 繪制折線圖
# 通過label屬性設置圖例
# 通過linestyle屬性設置線條樣式 -實線 --虛線 -.點劃線 :點虛線 ' '無線條
# 通過color屬性設置線條顏色 r紅色 g綠色 b藍色 w白色 c青色 m洋紅 y黃色 k黑色 支持16進制
# 通過alpha屬性設置線條的不透明程度
plt.plot(x, y_a, label='自己', linestyle='--', color='r', alpha=0.1)
plt.plot(x, y_b, label='同桌', linestyle='-', color='b', alpha=1)

# 顯示圖例
# 通過loc屬性設置圖形顯示的位置,可選值如下:
# best
# upper right
# upper left
# lower right
# lower left
# right
# center right 
# center left
# lower center
# upper center
# center
plt.legend(prop=myfont, loc='best')

# 設置刻度
x_ticks = ['{}歲'.format(i) for i in x]

# 顯示刻度
plt.xticks(x, x_ticks, fontproperties=myfont, rotation=15)

# 顯示圖形
plt.show()

繪制散點圖

假設通過爬蟲你獲取到了北京2016年3,10月份每天白天的最高氣溫(分別位于列表a,b),那么此時如何尋找出氣溫和隨時間(天)變化的某種規(guī)律?

a = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]

b = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]

from matplotlib import pyplot as plt
from matplotlib import font_manager

plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')

x_a = range(1, 32)
x_b = range(1+50, 32+50)
y_a = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17,
       18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]
y_b = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17,
       20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]

plt.scatter(x_a, y_a, label='3月份')
plt.scatter(x_b, y_b, label='10月份')


plt.legend(loc="upper left", prop=myfont)

x = list(x_a)+list(x_b)
x_ticks = ['3月{}日'.format(i) for i in x_a]
x_ticks += ['10月{}日'.format(i-50) for i in x_b]

plt.xticks(x[::3], x_ticks[::3], fontproperties=myfont, rotation=45)

plt.xlabel('日期', fontproperties=myfont)
plt.ylabel('最高溫度', fontproperties=myfont)
plt.title('三月和十月每日最高溫度變化', fontproperties=myfont)


plt.show()

繪制條形圖

假設你獲取到了2017年內(nèi)地電影票房前20的電影(列表a)和電影票房數(shù)據(jù)(列表b),那么如何更加直觀的展示該數(shù)據(jù)?
a = ["戰(zhàn)狼2", "速度與激情8", "功夫瑜伽", "西游伏妖篇", "變形金剛5:最后的騎士", "摔跤吧!爸爸", "加勒比海盜5:死無對證", "金剛:骷髏島", "極限特工:終極回歸", "生化危機6:終章", "乘風破浪", "神偷奶爸3", "智取威虎山", "大鬧天竺", "金剛狼3:殊死一戰(zhàn)", "蜘蛛俠:英雄歸來", "悟空傳", "銀河護衛(wèi)隊2", "情圣", "新木乃伊", ]

b = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23] 單位:億

橫向條形圖

from matplotlib import pyplot as plt
from matplotlib import font_manager

plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')


x = ["戰(zhàn)狼2", "速度與激情8", "功夫瑜伽", "西游伏妖篇", "變形金剛5:最后的騎士", "摔跤吧!爸爸", "加勒比海盜5:死無對證", "金剛:骷髏島", "極限特工:終極回歸",
     "生化危機6:終章", "乘風破浪", "神偷奶爸3", "智取威虎山", "大鬧天竺", "金剛狼3:殊死一戰(zhàn)", "蜘蛛俠:英雄歸來", "悟空傳", "銀河護衛(wèi)隊2", "情圣", "新木乃伊", ]

x = [i.replace(':', ':\n') for i in x]

y = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28,
     11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23]

plt.bar(range(len(x)), y)

plt.xticks(range(len(x)), x, fontproperties=myfont, rotation=45)

plt.xlabel('電影名', fontproperties=myfont)
plt.ylabel('票房(單位:億)', fontproperties=myfont)
plt.title('2017年內(nèi)地電影票房前20票房', fontproperties=myfont, fontsize=20)

plt.show()

縱向條形圖

from matplotlib import pyplot as plt
from matplotlib import font_manager

plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname='C:/Windows/Fonts/simhei.ttf')


x = ["戰(zhàn)狼2", "速度與激情8", "功夫瑜伽", "西游伏妖篇", "變形金剛5:最后的騎士", "摔跤吧!爸爸", "加勒比海盜5:死無對證", "金剛:骷髏島", "極限特工:終極回歸",
     "生化危機6:終章", "乘風破浪", "神偷奶爸3", "智取威虎山", "大鬧天竺", "金剛狼3:殊死一戰(zhàn)", "蜘蛛俠:英雄歸來", "悟空傳", "銀河護衛(wèi)隊2", "情圣", "新木乃伊", ]

y = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28,
     11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23]

plt.barh(range(len(x)), y)

plt.yticks(range(len(x)), x, fontproperties=myfont)

plt.xlabel('票房(單位:億)', fontproperties=myfont)
plt.ylabel('電影名', fontproperties=myfont)
plt.title('2017年內(nèi)地電影票房前20票房', fontproperties=myfont, fontsize=20)

plt.show()

繪制多個圖例的條形圖

假設你知道了列表a中電影分別在2017-09-14(b_14), 2017-09-15(b_15), 2017-09-16(b_16)三天的票房,為了展示列表中電影本身的票房以及同其他電影的數(shù)據(jù)對比情況,應該如何更加直觀的呈現(xiàn)該數(shù)據(jù)?
a = ["猩球崛起3:終極之戰(zhàn)", "敦刻爾克", "蜘蛛俠:英雄歸來", "戰(zhàn)狼2"]

b_16 = [15746, 312, 4497, 319]

b_15 = [12357, 156, 2045, 168]

b_14 = [2358, 399, 2358, 362]

from matplotlib import pyplot as plt
from matplotlib import font_manager

plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname="C:/Windows/Fonts/simhei.ttf")


a = ["猩球崛起3:終極之戰(zhàn)", "敦刻爾克", "蜘蛛俠:英雄歸來", "戰(zhàn)狼2"]
b_14 = [2358, 399, 2358, 362]
b_15 = [12357, 156, 2045, 168]
b_16 = [15746, 312, 4497, 319]

bar_width = 0.2
x_14 = list(range(len(a)))
x_15 = [i+bar_width for i in x_14]
x_16 = [i+bar_width*2 for i in x_14]

plt.bar(x_14, b_14, width=bar_width, label='2017-09-14')
plt.bar(x_15, b_15, width=bar_width, label='2017-09-15')
plt.bar(x_16, b_16, width=bar_width, label='2017-09-16')

plt.legend()

plt.xticks(x_15, a, fontproperties=myfont)

plt.xlabel('電影', fontproperties=myfont)
plt.ylabel('票房', fontproperties=myfont)
plt.title('電影票房', fontproperties=myfont, fontsize=20)

plt.show()

繪制直方圖

假設你獲取了250部電影的時長(列表a中),希望統(tǒng)計出這些電影時長的分布狀態(tài)(比如時長為100分鐘到120分鐘電影的數(shù)量,出現(xiàn)的頻率)等信息,你應該如何呈現(xiàn)這些數(shù)據(jù)?

a = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102, 123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]

from matplotlib import pyplot as plt
from matplotlib import font_manager


plt.figure(figsize=(20, 8), dpi=80)

myfont = font_manager.FontProperties(fname="C:/Windows/Fonts/simhei.ttf")

a = [131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136, 123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102, 123, 107, 143, 115, 136, 118, 139, 123, 112,
     118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135, 115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92, 121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113, 134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101, 131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]

# 組距
d = 3
# 計算組數(shù)
num_bins = (max(a)-min(a))//d

# 通過normed屬性,設置y軸顯示百分比
plt.hist(a, num_bins, normed=True)

plt.xticks(range(min(a), max(a)+d, d))

plt.xlabel('電影時長(分鐘)', fontproperties=myfont)
plt.ylabel('占比(%)', fontproperties=myfont)
plt.title('電影時長在各個時間段的比例', fontproperties=myfont, fontsize=20)

# 繪制網(wǎng)格
plt.grid()

plt.show()

更多繪圖工具

plot:可視化工具中的gitbub,相比于matplotlib更加簡單,圖形更加漂亮,同時兼容matplotlibpandas

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 目前能ping通的IP:216.58.193.51 59.18.44.245 59.18.44.53 59.18....
    StevenZack閱讀 2,422評論 0 0
  • matplotlib的基本使用——學習筆記 1、matplotlib簡介 ? matplotlib是Pytho...
    嚴簡易閱讀 1,215評論 0 2
  • 數(shù)據(jù)分析 1.matplotlib折線圖 matplotlib:最流行的Python底層繪圖庫,主要做數(shù)據(jù)可視化圖...
    Aptitude閱讀 729評論 0 1
  • 發(fā)現(xiàn)今天的自己又浪費的一天,給自己找的借口竟讓是感冒。 早上起來,發(fā)現(xiàn)外面一片的白色,三月的哈爾濱,卻是飄起了雪花...
    yi84095閱讀 252評論 1 0
  • 夏天,是綠色的清新;也是藍色的,像海的顏色,有一種憂郁。 兒時的夏天,不在乎炎熱,不在乎會被曬黑,隨著早升起的太陽...
    阿瑟流士閱讀 464評論 0 0

友情鏈接更多精彩內(nèi)容