1 繪制普通圖像
2 繪制柱狀圖
3 繪制散點圖
(1)繪制普通圖像
demo1
import matplotlib.pyplot as plt
import numpy as np
# 繪制普通圖像
x = np.linspace(-1, 1, 50)
y = 2 * x + 1
plt.plot(x, y)
plt.show()
# 繪制普通圖像
y = x**2
plt.plot(x, y)
plt.show()
結果
2017-12-14 16-30-36 的屏幕截圖.png
demo2
# figure的使用
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
# figure 1
plt.figure()
plt.plot(x, y1)
# figure 2
y2 = x**2
plt.figure()
plt.plot(x, y2)
# figure 3,指定figure的編號并指定figure的大小, 指定線的顏色, 寬度和類型
y2 = x**2
plt.figure(num = 5, figsize = (4, 4))
plt.plot(x, y1)
plt.plot(x, y2, color = 'red', linewidth = 1.0, linestyle = '--')
plt.show()
結果
2017-12-14 16-41-46 的屏幕截圖.png
2017-12-14 16-41-56 的屏幕截圖.png
demo3 設置坐標軸
import matplotlib.pyplot as plt
import numpy as np
# 繪制普通圖像
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x**2
plt.figure()
plt.plot(x, y1)
plt.plot(x, y2, color = 'red', linewidth = 1.0, linestyle = '--')
# 設置坐標軸的取值范圍
plt.xlim((-1, 1))
plt.ylim((0, 2))
# 設置坐標軸的lable
plt.xlabel('X axis')
plt.ylabel('Y axis')
# 設置x坐標軸刻度, 原來為0.25, 修改后為0.5
plt.xticks(np.linspace(-1, 1, 5))
# 設置y坐標軸刻度及標簽, $$是設置字體
plt.yticks([0, 0.5], ['$minimum$', 'normal'])
plt.show()
結果
2017-12-14 16-48-27 的屏幕截圖.png
demo4 設置legend圖例
import matplotlib.pyplot as plt
import numpy as np
# 繪制普通圖像
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x**2
plt.figure()
# 在繪制時設置lable, 逗號是必須的
l1, = plt.plot(x, y1, label = 'line')
l2, = plt.plot(x, y2, label = 'parabola', color = 'red', linewidth = 1.0, linestyle = '--')
# 設置坐標軸的取值范圍
plt.xlim((-1, 1))
plt.ylim((0, 2))
# 設置坐標軸的lable
plt.xlabel('X axis')
plt.ylabel('Y axis')
# 設置x坐標軸刻度, 原來為0.25, 修改后為0.5
plt.xticks(np.linspace(-1, 1, 5))
# 設置y坐標軸刻度及標簽, $$是設置字體
plt.yticks([0, 0.5], ['$minimum$', 'normal'])
# 設置legend
plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')
plt.show()
結果:
2017-12-14 17-03-34 的屏幕截圖.png
(2)繪制柱狀圖
demo1基本柱狀圖
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.bar(range(len(data)), data)
plt.show()
結果
2017-12-14 17-06-32 的屏幕截圖.png
plt.bar 函數簽名為:
bar(left, height, width=0.8, bottom=None, **kwargs)
事實上,left,height,width,bottom這四個參數確定了柱體的位置和大小。默認情況下,left為柱體的居中位置(可以通過align參數來改變left值的含義
demo2設置柱體樣式
通過 facecolor(或fc) 關鍵字參數可以設置柱體顏色,例如:
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.bar(range(len(data)), data, fc='g')
plt.show()
2017-12-14 17-16-27 的屏幕截圖.png
通過 color 關鍵字參數 可以一次性設置多個顏色,例如:
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.bar(range(len(data)), data, color='rgb') # or `color=['r', 'g', 'b']`
plt.show()
2017-12-14 17-17-00 的屏幕截圖.png
demo3 設置tick label
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']
plt.bar(range(len(data)), data, tick_label=labels)
plt.show()
file:///home/tong/%E5%9B%BE%E7%89%87/2017-12-14%2017-18-17%20%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png
demo3 并列柱狀圖
import numpy as np
import matplotlib.pyplot as plt
size = 5
x = np.arange(size)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
total_width, n = 0.8, 3
width = total_width / n
x = x - (total_width - width) / 2
plt.bar(x, a, width=width, label='a')
plt.bar(x + width, b, width=width, label='b')
plt.bar(x + 2 * width, c, width=width, label='c')
plt.legend()
plt.show()
2017-12-14 17-19-00 的屏幕截圖.png
(3)繪制散點圖
demo1
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y)
plt.show()
2017-12-14 17-23-31 的屏幕截圖.png
demo2
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
color = ['r','y','k','g','m']
plt.scatter(x, y,c=color,marker='>')
plt.show()
2017-12-14 17-24-35 的屏幕截圖.png
demo3
alpha:標量,可選,默認值:無, 0(透明)和1(不透明)之間的alpha混合值
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y,alpha=0.5)
plt.show()
2017-12-14 17-25-03 的屏幕截圖.png