python—繪圖模塊matplotlib

1.1 安裝

https://dev.windows.com/ 下載Visual Studio Community

https://pypi.python.org/pypi/matplotlib/ 下載matplotlib安裝程序,查找與使用的python版本匹配的wheel文件

打開命令窗口,切換到項目文件夾使用pip安裝matplotlib

 python -m pip install --user matplotlib-2.2.2-cp36-win32.whl

1.2 測試

使用python3 IDLE 嘗試導入matplotlib

import matplotlib

如果沒出現任何錯誤信息,安裝成功。

2.1 繪製簡單的折線圖

首先導入matplotlib.pyplot,
模塊pyplot中包含很多用於生成圖表的函數,
將數據列表傳遞給函數plot(),這個函數常識根據這些數字繪製有意義的圖形,
plt.show()打開matplotlib查看器

from matplotlib import pyplot as plt

squares = [1, 4, 9, 16, 25, 36]
plt.plot(squares)
plt.show()

在這裏插入圖片描述

2.2 修改標籤文字和線條粗細

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25, 36]
plt.plot(squares, linewidth=5, color='green')				# 設置線條粗細和顏色

plt.title('Squares Number', fontsize=14)                    # 設置標題
plt.xlabel('Number', fontsize=10, color='red')               # 設置x軸標題
plt.ylabel('Squares of Number', fontsize=10, color='blue')   # 設置y軸標題

plt.tick_params(axis='both', labelsize=10, colors='gold')    # 設置刻度尺樣式

plt.show()

在這裏插入圖片描述
.tick_params()參數設置:

axis='x'設置x軸刻度樣式
axis='y'設置y軸刻度樣式
width=2,設置刻度粗細
which='major'/ 'minor'/ 'both' 表示設置主刻度,副刻度,同時設置
direction='in'/ 'out'/ 'inout' 設置刻度在軸內,軸外,內外都顯示
lenth=5,設置刻度長度
pad=5,設置刻度與值的距離
color, labelcolor, colors分別設置刻度顏色,值的顏色,同時設置顏色
labelsize設置值的字體大小
top, bottom, left, right=True/False,爲布爾值,表示各方向邊框的刻度線是否顯示
labeltop, labelbottom, labelleft, labelright=True/False,表示各方向邊框的值是否顯示

2.3 校正圖形

給.plot()提供值時,它默認第一個數據對應的x座標值爲0,但我們的第一個點對應的x爲1,我們要給.plot同時提供輸入值和輸出值

import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5, 6]
squares = [1, 4, 9, 16, 25, 36]
plt.plot(input_values, squares, linewidth=5, color='green')

plt.title('Squares Number', fontsize=14)
plt.xlabel('Number', fontsize=6, color='red')
plt.ylabel('Squares of Number', fontsize=6, color='blue')

plt.tick_params(axis='both', labelsize=6, colors='gold')

plt.show()

在這裏插入圖片描述
2.4 用scatter()繪製散點圖並設置其樣式

import matplotlib.pyplot as plt

plt.scatter(2, 4, s=50)   # (2, 4)爲座標,s設置點的大小
plt.title('Squares Number', fontsize=20)
plt.xlabel('Value', fontsize=10)
plt.ylabel('Squares of Value', fontsize=10)

plt.tick_params(axis='both', which='maior', labelsize=14)

plt.show()

在這裏插入圖片描述
2.5 使用scatter繪製一系列的點

設置x,y對應的值繪製多個點

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=50)
plt.title('Squares Number', fontsize=20)
plt.xlabel('Value', fontsize=10)
plt.ylabel('Squares of Value', fontsize=10)

plt.tick_params(axis='both', which='maior', labelsize=14)

plt.show()

在這裏插入圖片描述
2.6 繪製多個點時,利用函數完成

import matplotlib.pyplot as plt

x_values = range(0, 1000)                 # 設置x取值
y_values = [x ** 2 for x in x_values]     # 設置y取值
plt.scatter(x_values, y_values, s=10)
plt.title('Squares Number', fontsize=20)
plt.xlabel('Value', fontsize=10)
plt.ylabel('Squares of Value', fontsize=10)

plt.axis([0, 1001, 0, 1000001])           # 設置x,y軸範圍
plt.tick_params(axis='both', which='maior', labelsize=14)

plt.show()

在這裏插入圖片描述
2.7 自定義顏色
.scatter(c=(0.1, 0.5, 0.2))函數參數c指定點的顏色,也可以用.scatter(c=‘red’)

import matplotlib.pyplot as plt

x_values = range(0, 1000)
y_values = [x ** 2 for x in x_values]
plt.scatter(x_values, y_values, c=(0, 1, 1), s=10)
plt.title('Squares Number', fontsize=20)
plt.xlabel('Value', fontsize=10)
plt.ylabel('Squares of Value', fontsize=10)

plt.axis([0, 1001, 0, 1000001])
plt.tick_params(axis='both', which='maior', labelsize=14)

plt.show()

c中的值,越接近0,顏色越深,越接近1,顏色越淺
在這裏插入圖片描述
2.8 使用顏色映射

.scatter(c=y_values, cmap=plt.cm.Reds) ,c設置爲y值列表,cmap告訴pyplot使用哪個顏色映射

import matplotlib.pyplot as plt

x_values = range(0, 1000)
y_values = [x ** 2 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Reds, s=10)
plt.title('Squares Number', fontsize=20)
plt.xlabel('Value', fontsize=10)
plt.ylabel('Squares of Value', fontsize=10)

plt.axis([0, 1001, 0, 1000001])
plt.tick_params(axis='both', which='maior', labelsize=14)

plt.show()

在這裏插入圖片描述

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