Python中matplotlib模塊plot用法,實現繪畫折線,點狀等圖


每天記錄一下編程中遇到的問題

1.語法

 

plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

 

根據x,y數據,實現折線或點狀圖。
x:橫坐標數據,可選參數。
y:縱坐標數據。
fmt:可選參數,定義圖形基本格式,如顏色、標記和線型。

  

2.簡單用法

 
         
import matplotlib.pyplot as plt

# 這兩行代碼解決 plt 中文顯示的問題
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# X軸坐標值
x_labels = ['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19']

# Y軸坐標值
y_data1 = [100, 800, 50, 200, 150, 300, 100, 10]
y_data2 = [600, 200, 250, 150, 700, 400, 300, 500]
y_data3 = [50, 300, 200, 100, 1000, 200, 300, 250]

# 默認折線圖
plt.plot(x_labels, y_data1)

# 折角是藍色圓點折線圖
# plt.plot(x_labels, y_data1, 'bo')

# 以Y軸索引為X軸值實現折線圖
# plt.plot(y_data1)

# 點狀圖
# plt.plot(y_data1, 'c.')
plt.show()
默認折線圖 plt.plot(x_labels, y_data1)
# plt.show()

 

3.plot支持多個參數,如color(顏色),marker(標記點),linestyle(線條風格),linewidth(線寬),markersize(標記點大小)等。

把這些參數都加進去看一下效果。

plt.plot(x_labels, y_data1, color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)

效果圖展示:

 

 

 

4.x,y數據支持對象輸入,如字典(dict),pandas.DataFame,numpy

y_data4 = {"x_labels": ['2021-2-12', '2021-2-13', '2021-2-14',
                        '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
           "y_test": [600, 200, 250, 150, 700, 400, 300, 500]}
# 獲取y_data4字典x_labels為X軸,y_test為Y軸

plt.plot("x_labels", "y_test", data=y_data4)

 

5.支持多條圖型展示

一般我們做表格時,不僅是一條折線圖或點狀圖,可能會有二條,三條甚至多條,遇到這種情況,我們plot函數支持兩種方式實現。

1)多次調用plot函數。

plt.plot(x_labels, y_data1)
plt.plot(x_labels, y_data2)
plt.plot(x_labels, y_data3)

2)原數據為二維數組。

data5 = [['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
         [600, 200, 250, 150, 700, 400, 300, 500],
         [50, 300, 200, 100, 1000, 200, 300, 250],
         [100, 800, 50, 200, 150, 300, 100, 10]]

# 展示多條折線圖2, 若data5是二維數組,其中第一列為X軸,其余列為Y軸
plt.plot(data5[0], data5[1], data5[2], data5[3])

3)指定 [x]y[fmt] 多個集合。

plt.plot(x_labels, y_data1, 'r--', x_labels, y_data2, '-g', x_labels, y_data3, '--')

 

6.[fmt]可選參數介紹

fmt = '[marker][line][color]'

 這里僅列出部分參考值。

Markers

 

Line Styles

 

如:

'b'    # blue markers with default shape
'or'   # red circles
'-g'   # green solid line
'--'   # dashed line with default color
'^k:'  # black triangle_up markers connected by a dotted line

Colors

 

完整代碼:

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Date     :2021/2/20 11:22
# @Author   :wmzhong
# @Email    :wmzhong_01@163.com
# @Bolg     :https://www.cnblogs.com/wmzhong/
# @jianshu  :https://www.jianshu.com/u/2ef83f0891c7
# @Description:折線,點狀圖
-------------------------------------------------
"""

import matplotlib.pyplot as plt

# 這兩行代碼解決 plt 中文顯示的問題
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# X軸坐標值
x_labels = ['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19']

# Y軸坐標值
y_data1 = [100, 800, 50, 200, 150, 300, 100, 10]
y_data2 = [600, 200, 250, 150, 700, 400, 300, 500]
y_data3 = [50, 300, 200, 100, 1000, 200, 300, 250]
y_data4 = {"x_labels": ['2021-2-12', '2021-2-13', '2021-2-14',
                        '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
           "y_test": [600, 200, 250, 150, 700, 400, 300, 500]}
data5 = [['2021-2-12', '2021-2-13', '2021-2-14', '2021-2-15', '2021-2-16', '2021-2-17', '2021-2-18', '2021-2-19'],
         [600, 200, 250, 150, 700, 400, 300, 500],
         [50, 300, 200, 100, 1000, 200, 300, 250],
         [100, 800, 50, 200, 150, 300, 100, 10]]

# 設置Y軸標題
plt.ylabel("零花錢/元")

# 標題
plt.title("過年得壓歲錢調查結果")

# 默認折線圖
# plt.plot(x_labels, y_data1)

# 折角是藍色圓點折線圖
# plt.plot(x_labels, y_data1, 'bo')

# 以Y軸索引為X軸值實現折線圖
# plt.plot(y_data1)

# 點狀圖
# plt.plot(y_data1, 'c.', linestyle='dashed')
# plt.plot(x_labels, y_data1, color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)

# 獲取y_data4字典x_labels為X軸,y_test為Y軸
# plt.plot("x_labels", "y_test", data=y_data4)

# 展示多條折線圖1
# plt.plot(x_labels, y_data1)
# plt.plot(x_labels, y_data2)
# plt.plot(x_labels, y_data3)

# 展示多條折線圖2, 若data5是二維數組,其中第一列為X軸,其余列為Y軸
# plt.plot(data5[0], data5[1], data5[2], data5[3])

# 展示多條折現圖3,指定 [x], y, [fmt] 多個集合
# 設置X坐標尺寸大小,不設置會導致X軸顯示過於擁擠,適度設置。
plt.tick_params(labelsize=8)
plt.plot(x_labels, y_data1, 'r--', x_labels, y_data2, '-g', x_labels, y_data3, '--')

# 設置圖標
plt.legend(labels=["小明", "小紅", "小華"])
plt.show()

 

參考資料:https://matplotlib.org/3.1.1/api/pyplot_summary.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM