Matplotlib 線形圖

一、傳數據的兩種方式

matplotlib.pyplot.plot(args, scalex=True, scaley=True, data=None, kwargs)

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.plot([2, 2, 2, 2])

plt.show()

在這裏插入圖片描述

fig, ax = plt.subplots()

data = {'a': [1, 2, 3], 'b': [3, 2, 1]}
ax.plot('a', 'b', data=data)

plt.show()

在這裏插入圖片描述

二、繪製多條線

fig, ax = plt.subplots()

data = {'a': [1, 2, 3], 'b': [3, 2, 1]}

ax.plot('a', 'b', data=data)
ax.plot([2, 2, 2, 2])

plt.show()

在這裏插入圖片描述

三、其它設置

1、顏色 —— color

可選值: ‘b’ blue ‘g’ green ‘r’ red ‘c’ cyan ‘m’ magenta ‘y’ yellow ‘k’ black ‘w’ white

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', drawstyle='steps')

plt.show()

在這裏插入圖片描述

2、線性 —— linestyle¶

可以使用字符串“ solid”,“ dotted”,“ dashed”或“ dashdot”定義簡單的線型。通過提供破折號元組可以實現更精細的控制 。

可選值:[‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | ‘-’ | ‘–’ | ‘-.’ | ‘:’ | ‘None’ | ’ ’ | ‘’]
在這裏插入圖片描述

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', linestyle='dotted')

plt.show()

在這裏插入圖片描述

3、drawstyle

設置繪圖的繪製樣式。繪製樣式確定點的連接方式。

參數:
可選值:[‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]

默認值:‘default’ 對於“默認”,這些點用直線連接。

階梯變體將點與階梯狀線(即具有垂直階梯的水平線)相連。它們在步驟的位置上有所不同:

  • ‘steps-pre’:步驟位於線段的開頭,即線將位於右側點的y值。
  • ‘steps-mid’:步驟在兩點之間進行。
  • 'steps-post:步驟位於線段的末端,即線將位於左側點的y值。
  • “ steps”等於“ steps-pre”,並且爲了向後兼容而進行維護。
fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], color='r', drawstyle='steps')

plt.show()

在這裏插入圖片描述

4、標記 —— marker

可選參數:
在這裏插入圖片描述

點擊查看更多參數

None是默認值,表示“無”,但是從其他文檔中引用了此表以獲取標記輸入的有效輸入,在這些情況下,None仍表示“默認值”。

fig, ax = plt.subplots()

ax.plot([1, 2, 3, 4, 5, 6], marker='o')

plt.show()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章