matplotlib畫圖總結--常用功能


0、內容范圍

    多曲線圖、圖例、坐標軸、注釋文字等。

1、曲線圖

  多曲線圖、圖例、網格、坐標軸名稱、圖標名、坐標軸范圍等。

from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 200, endpoint=True)
c, s = np.cos(x), np.sin(x)
plt.xlim(-np.pi, np.pi)
# p1 = plt.plot(x,c,'r', label = 'cos')
# p2 = plt.plot(x,s,'b', label =  'sin')
p1 = plt.plot(x,c,'r')
p2 = plt.plot(x,s,'b')
plt.xlabel('x')
plt.ylabel('y')
plt.title('cos and sin')
plt.legend( ['cos', 'sin'])
plt.grid(True)
plt.show()

 知識點:

1)一個圖里繪制條曲線

方法1:

plt.plot(x,c,'r', x,s,'b')

方法2:

p1 = plt.plot(x,c,'r')

p2 = plt.plot(x,s,'b')

2)給圖添加圖例

方法1:

 p1 = plt.plot(x,c,'r', label = 'cos')

 p2 = plt.plot(x,s,'b', label =  'sin')

plt.legend()

方法2:

p1 = plt.plot(x,c,'r')

p2 = plt.plot(x,s,'b')

plt.legend( ['cos', 'sin'])

推薦使用方法1,因為方法2必須注意添加曲線的順序。

 

 2、柱狀圖

   學習柱狀圖、為圖形添加字符坐標軸.

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd, label='men')
p2 = plt.bar(ind, womenMeans, width, bottom=menMeans, yerr=womenStd, label='women')

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend(loc='upper right')
plt.show()

 為什么會兩個圖層疊?

p1 = plt.bar(ind, menMeans, width, yerr=menStd, label='men')
p2 = plt.bar(ind, womenMeans, width, bottom=menMeans, yerr=womenStd, label='women')

bar函數的x坐標一樣,並且y坐標值是bottom=menMeans,所以第二個圖會堆疊在第一個圖上面。

下面實現2條柱狀圖。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

plt.show()

 x軸的坐標不是數子,實現方法如下,先設定坐標軸的tick,然后把ticklabel改為字符值。

ax.set_xticks(x)

ax.set_xticklabels(labels)

如果需要實現水平柱狀圖,則使用ax.barh(y_pos, performance, xerr=error, align='center')函數。

 

3、添加文字、注釋、箭頭指示、花式文字

其中,text 函數可以做文本注釋,且支持 LaTeX 格式,可以在圖例中寫公式。

text(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")

x = np.arange(0, 10, 0.1)
plt.plot(x, x**2)
plt.grid(True)  # 設置網格線
plt.text(5,50, "TEXT1")
plt.show()

花式文本:

You can put a rectangular box around the text instance (e.g., to set a background color) by using the keyword bbox. bbox is a dictionary of Rectangle properties. For example:

>>> text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))

import matplotlib.pyplot as plt
import numpy as np

plt.text(0.6, 0.5, "Text", size=50, rotation=45, ha="center", va="center", color='b')
plt.text(0.3, 0.5, "Text", size=25, rotation=10, ha="center", va="center", bbox=dict(boxstyle="round",ec=(1, 0.5, 0.5),fc=(1., 0.8, 0.8),)) #顏色ec、fc

plt.plot()
plt.show()

添加箭頭和說明信息

plt.annotate('value of 5**2', xy=(5, 25), xytext=(6, 26), arrowprops=dict(facecolor='black', shrink=0.05))


免責聲明!

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



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