Matplotlib

pandas提供了一些用于將表格型數(shù)據(jù)讀取為DataFrame對(duì)象的函數(shù)嘁灯,期中read_csv和read_table這兩個(gè)使用最多

  • 使用read_csv讀取
  • 使用read_table讀取
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
df1 = pd.read_csv('./data/type_line')  # read_csv默認(rèn)是按照啥容,去分割的 遇到-不分割 當(dāng)成了一塊內(nèi)容
df1 = pd.read_csv('./data/type_line',sep='-')  # 如果不是逗號(hào)分割的 需要指定分隔符
# 默認(rèn)會(huì)把第一行當(dāng)成列名 如果第一行不是列名 是內(nèi)容 可以設(shè)置header參數(shù)為None
df1 = pd.read_csv('./data/type_line',sep='-',header=None) 
df1.shape
df1
# 用來讀取 table separated value tsv文件 是用\t分割的一系列值
pd.read_table('./data/wheats.tsv')
pd.read_table('./data/wheats.tsv',header=None)
# read_table也可以讀其他類型的文件 只不過要指定分隔符
pd.read_table('./data/type_comma')
pd.read_table('./data/type_comma',sep=',')
pd.read_csv('./data/wheats.tsv',sep='\t',header=None)

讀寫excel

使用 pd.read_excel() 讀取excel表格
使用 df.to_excel() 輸出excel表格
可能需要安裝庫(kù) >>> conda install openpyxl >>> conda install xlsxwriter >>> conda install xlrd

df2 = pd.read_excel('./data/jfeng.xlsx')
df2
# 要保存哪個(gè)DataFrame 就用這個(gè)DataFrame調(diào)用 to_excel方法 (注意是DataFrame對(duì)象 不是pandas模塊) 
df2.to_excel('./jfeng.xlsx')

讀取sqlite文件(其他數(shù)據(jù)庫(kù)文件也是類似的操作 這里以sqlite為例)

導(dǎo)包 import sqlite3

import sqlite3

連接數(shù)據(jù)庫(kù)
sqlite3.connect('dbpath')

讀取table內(nèi)容
pd.read_sql("SQL語句", con)

寫入數(shù)據(jù)庫(kù)文件 df對(duì)象.to_sql('table_name',connection)

操作數(shù)據(jù)庫(kù) connection.execute(SQL語句)

connection = sqlite3.connect('./data/weather_2012.sqlite')
connection
# 數(shù)據(jù)庫(kù)可視化工具 mysql-Navicat MongoDB-robot 3T sqllite-SQLite Expert Personal
# sql, con, index_col=None
# 查詢: 傳入查詢語句 連接對(duì)象 設(shè)置索引列
pd.read_sql('select * from weather_2012',connection)
pd.read_sql('select * from weather_2012',connection,index_col='index')
# 創(chuàng)建表格 把df2這個(gè)表格存入數(shù)據(jù)庫(kù)
# 參數(shù) name, con 要新建的表格的名字 連接對(duì)象
df2.to_sql('jfeng',connection)
# 刪除表格
connection.execute('drop table jfeng')  # drop table 要?jiǎng)h除的表格名

使用read_csv直接讀取網(wǎng)絡(luò)上的數(shù)據(jù)

url = 'https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv'

url = 'https://raw.githubusercontent.com/datasets/investor-flow-of-funds-us/master/data/weekly.csv'
pd.read_csv(url)

透視表

各種電子表格程序和其他數(shù)據(jù)分析軟件中一種常見的數(shù)據(jù)匯總工具辙培。它根據(jù)一個(gè)或多個(gè)鍵對(duì)數(shù)據(jù)進(jìn)行聚合蓖柔,并根據(jù)行和列上的分組鍵將數(shù)據(jù)分配到各個(gè)矩形區(qū)域中

data = np.random.randint(60,100,size=(6,2))
columns = ['height','weight']
df = DataFrame(data=data,columns=columns)
df['age'] = [20,18,30,26,22,32]
df['smoke'] = [True,False,True,False,False,False]
df['sex'] = ['male','female','male','male','female','female']
df

行分組透視表 設(shè)置index參數(shù)

列分組透視表 設(shè)置columns參數(shù)

行列分組的透視表 同時(shí)設(shè)定index、columns參數(shù)

fill_value:替換結(jié)果中的缺失值

# 男性的平均年齡 和 女性的平均年齡

df.groupby('sex').groups
df.groupby('sex')['age'].mean()
df.groupby('sex')[['age','height','smoke','weight']].mean()
# 透視表
pd.pivot_table(df,index='sex')
pd.pivot_table(df,columns='sex')

交叉表

是一種用于計(jì)算分組頻率的特殊透視圖,對(duì)數(shù)據(jù)進(jìn)行匯總

pd.crosstab(index,colums)

  • index:分組數(shù)據(jù)侮叮,交叉表的行索引
  • columns:交叉表的列索引
# 男性和女性 吸煙和不吸煙 的人數(shù)
pd.crosstab(index=df.sex,columns=df.smoke)
pd.crosstab(index=df.smoke,columns=df.sex)

matplotlib

目錄

  • 一、Matplotlib基礎(chǔ)知識(shí)

  • 二、設(shè)置plot的風(fēng)格和樣式

    • 1撩嚼、點(diǎn)和線的樣式
    • 2、同時(shí)設(shè)置多個(gè)曲線樣式
    • 3挖帘、曲線樣式的三種設(shè)置方式
    • 4完丽、X、Y軸坐標(biāo)刻度
  • 三拇舀、2D圖形

    • 1逻族、示例
    • 2、【重點(diǎn)】直方圖
    • 3骄崩、【重點(diǎn)】條形圖
    • 4聘鳞、【重點(diǎn)】餅圖
    • 5、【重點(diǎn)】散點(diǎn)圖

=============以上為重點(diǎn)=================

  • 四刁赖、圖形內(nèi)的文字搁痛、注釋、箭頭

    • 1宇弛、圖形內(nèi)的文字
    • 2鸡典、注釋
    • 3、箭頭
  • 五枪芒、3D圖

    • 1彻况、曲面圖

一谁尸、Matplotlib基礎(chǔ)知識(shí)

Matplotlib中的基本圖表包括的元素

  • x軸和y軸 axis 水平和垂直的軸線

  • 軸標(biāo)簽 axisLabel 水平和垂直的軸標(biāo)簽

  • x軸和y軸刻度 tick 刻度標(biāo)示坐標(biāo)軸的分隔,包括最小刻度和最大刻度

  • x軸和y軸刻度標(biāo)簽 tick label 表示特定坐標(biāo)軸的值

  • 繪圖區(qū)域(坐標(biāo)系) axes 實(shí)際繪圖的區(qū)域

  • 畫布 figure 呈現(xiàn)所有的坐標(biāo)系

只含單一曲線的圖?

# 正弦曲線
# 先獲取x -π到π
x = np.arange(-np.pi,np.pi,0.1)  # [start,] stop[, step,] 開始值 結(jié)束值 步長(zhǎng)值
x
y = np.sin(x)
y
plt.plot(x,y)  # plot(x, y) 最簡(jiǎn)單的參數(shù)形式 傳入x和y的值

包含多個(gè)曲線的圖

1纽甘、可以使用多個(gè)plot函數(shù)(推薦)良蛮,在一個(gè)圖中繪制多個(gè)曲線

# 繪制兩個(gè)圖 一個(gè)是 -π到0 另一個(gè)是0到π
x1 = np.arange(-np.pi,0,0.1)
x2 = np.arange(0,np.pi,0.1)
# 方式一:多次使用 plt.plot繪制多個(gè)函數(shù)
plt.plot(x1,np.sin(x1))
plt.plot(x2,np.sin(x2))

2、也可以在一個(gè)plot函數(shù)中傳入多對(duì)X,Y值悍赢,在一個(gè)圖中繪制多個(gè)曲線

x1 = np.arange(-np.pi,0,0.1)
x2 = np.arange(0,np.pi,0.1)
# 方式二 調(diào)用一次plt.plot里面?zhèn)魅攵鄠€(gè)x和y的值 plt.plot(x1,y1,x2,y2,...xn,yn)
plt.plot(x1,np.sin(x1),x2,np.sin(x2))

子畫布?

# 先調(diào)整一下畫布大小
plt.figure(figsize=(8,8))
# For example, ``subplot(2, 3, 3)`` and ``subplot(233)`` 
# subplot(2, 3, 3) 創(chuàng)建一個(gè)子畫布 把原來的大的畫布 上下分成2部分 左右分成3部分 占用哪個(gè)部分
# axes1 = plt.subplot(2,2,1)  # 根據(jù)傳入的參數(shù)創(chuàng)建子畫布 返回坐標(biāo)系對(duì)象
# axes2 = plt.subplot(2,2,2)
# axes3 = plt.subplot(2,2,3)
# axes4 = plt.subplot(2,2,4)
axes1 = plt.subplot(4,2,1)  # 根據(jù)傳入的參數(shù)創(chuàng)建子畫布 返回坐標(biāo)系對(duì)象
axes2 = plt.subplot(4,2,4)
axes3 = plt.subplot(2,2,3)
axes4 = plt.subplot(2,2,4)
# 接下來注意 是在每一個(gè)小的坐標(biāo)系上繪圖 而不是在大的畫布上繪圖 所以 是用對(duì)應(yīng)的axes對(duì)象來調(diào)用plot
axes1.plot(x,np.sin(x))
axes2.plot(x,np.cos(x))
axes3.plot(x,np.tan(x))
axes4.plot(x,np.tanh(x))

網(wǎng)格線

使用plt.grid方法可以開啟網(wǎng)格線决瞳,使用plt面向?qū)ο蟮姆椒ǎ瑒?chuàng)建多個(gè)子圖顯示不同網(wǎng)格線

  • axis顯示軸向
  • color代表顏色
  • alpha表示線的明暗程度
  • lw代表linewidth左权,線的粗細(xì)
plt.figure(figsize=(8,8))
axes1 = plt.subplot(221)
axes2 = plt.subplot(222)
axes3 = plt.subplot(223)
axes4 = plt.subplot(224)
# 給坐標(biāo)系添加網(wǎng)格
# plt.grid()  # 注意plt.grid()確實(shí)可以繪制網(wǎng)格線 但是如果有子畫布 plt.grid只會(huì)給離他最近的那個(gè)子畫布繪制網(wǎng)格線
axes1.grid()  # axis='both' 默認(rèn)水平豎直方向都有網(wǎng)格線
axes2.grid(axis='x')  # 在軸上方繪制網(wǎng)格線
axes3.grid(axis='y')
axes4.grid(color='red',linewidth=2,alpha=0.2)

坐標(biāo)軸的軸線

plt.axis([xmin,xmax,ymin,ymax])

# 繪制圓形
x = np.linspace(-1,1,100)
x
# x**2+y**2=1
# y**2=1-x**2
# y=(1-x**2)**0.5
y=(1-x**2)**0.5
plt.plot(x,y)
plt.plot(x,-y)
plt.axis([-2,2,-2,2])  # [x軸的起始值,x軸的結(jié)束值,y軸的起始值,y軸的結(jié)束值]
plt.axis([-1.5,1,-5,3])
# plt.axis([0.5,0.5,1,1])

plt.axis('xxx') 'off'皮胡、'equal'……

設(shè)置坐標(biāo)軸類型
關(guān)閉坐標(biāo)軸

x = np.linspace(-1,1,100)
x
# x**2+y**2=1
# y**2=1-x**2
# y=(1-x**2)**0.5
y=(1-x**2)**0.5
plt.plot(x,y)
plt.plot(x,-y)
plt.axis('equal')  # 讓x軸和y軸等長(zhǎng)
plt.axis('off')

xlim方法和ylim方法

除了plt.axis方法,還可以通過xlim赏迟,ylim方法設(shè)置坐標(biāo)軸范圍

x = np.linspace(-1,1,100)
x
# x**2+y**2=1
# y**2=1-x**2
# y=(1-x**2)**0.5
y=(1-x**2)**0.5
plt.plot(x,y)
plt.plot(x,-y)
plt.xlim([-3,3])
plt.ylim([-2,2])

坐標(biāo)軸的標(biāo)簽

plt.xlabel( )方法 和 plt.ylabel( )方法
例如 plt.ylabel('y = x^2 + 5',rotation = 60)

  • color 標(biāo)簽顏色
  • fontsize 字體大小
  • rotation 旋轉(zhuǎn)角度
# 正弦曲線
x = np.arange(-np.pi,np.pi,0.1)
x
y = np.sin(x)
plt.plot(x,y)
# s x標(biāo)簽的內(nèi)容
plt.xlabel('x')
plt.ylabel('f(x)=sin(x)')
plt.xlabel('x',color='orange',fontsize=20,rotation=20)
plt.ylabel('f(x)=sin(x)',rotation=20)
plt.ylabel('f(x)=sin(x)',rotation=90)  # y軸標(biāo)題 默認(rèn)就是旋轉(zhuǎn)了90度

畫布的標(biāo)題

plt.title()方法

  • loc 標(biāo)題位置{left,center,right}
  • color 標(biāo)題顏色
  • fontsize 字體大小
  • rotation 旋轉(zhuǎn)角度
# 正弦曲線
x = np.arange(-np.pi,np.pi,0.1)
x
y = np.sin(x)
plt.plot(x,y)
# 參數(shù) s標(biāo)題的內(nèi)容
# plt.title('Sin(x)')
# plt.title('Sin(x)',color='red',fontsize=25,rotation=45)
# loc : {'center', 'left', 'right'},
# plt.title('Sin(x)',color='red',fontsize=25,rotation=45,loc='left')
plt.title('Sin(x)',color='red',fontsize=25,rotation=45,loc='right')  # 默認(rèn)在中間

圖例

legend方法

兩種傳參方法:

  • 分別在plt.plot( )函數(shù)中增加label參數(shù),再調(diào)用plt.legend( )方法顯示
  • 直接在legend方法中傳入字符串列表 如:plt.legend(['normal','fast','slow'])
# 方式一 plt.plot()中傳入名字
x = np.linspace(0,10,101)
x
plt.plot(x,x,label='normal')
plt.plot(x,2*x,label='fast')
plt.plot(x,x/2,label='slow')
plt.legend()
# 方式二 把名字一起傳入 plt.legend()中
x = np.linspace(0,10,101)
x
plt.plot(x,x)
plt.plot(x,2*x)
plt.plot(x,x/2)
plt.legend(['normal','fast','slow'])

loc參數(shù)?

  • loc參數(shù)用于設(shè)置圖例的位置屡贺,一般在legend函數(shù)內(nèi)
  • matplotlib已經(jīng)預(yù)定義好幾種數(shù)字表示的位置
    字符串 數(shù)值 字符串 數(shù)值
    best 0 center left 6
    upper right 1 center right 7
    upper left 2 lower center 8
    lower left 3 upper center 9
    lower right 4 center 10
    right 5
    loc參數(shù)還可以是2元素的列表,表示圖例左下角的坐標(biāo)

[0,0] 左下
[0,1] 左上
[1,0] 右下
[1,1] 右上
圖例也可以超過圖的界限loc = (-0.1,0.9)

data = np.random.randint(0,100,size=(10,3))
df = DataFrame(data,columns=list('ABC'))
df
plt.plot(df['A'])
plt.plot(df['B'])
plt.plot(df['C'])
# loc參數(shù) The location of the legend 設(shè)置圖例的位置
# plt.legend(['1','2','3'],loc=0)  # 
# plt.legend(['1','2','3'],loc=1)
# plt.legend(['1','2','3'],loc=2)
# plt.legend(['1','2','3'],loc=10)
# plt.legend(['1','2','3'],loc=[1,1])
# plt.legend(['1','2','3'],loc=[0,1])
# plt.legend(['1','2','3'],loc=[0,0])
# plt.legend(['1','2','3'],loc=[1,0])
plt.legend(['1','2','3'],loc=[1.2,-0.5])

ncol參數(shù)

ncol控制圖例中有幾列,在legend中設(shè)置ncol,需要設(shè)置loc

data = np.random.randint(0,100,size=(10,3))
df = DataFrame(data,columns=list('ABC'))
df
plt.plot(df['A'])
plt.plot(df['B'])
plt.plot(df['C'])
# plt.legend(ncol=2)
plt.legend(ncol=3)

二锌杀、設(shè)置plot的風(fēng)格和樣式

plot語句中支持除X,Y以外的參數(shù)甩栈,以字符串形式存在,來控制顏色糕再、線型量没、點(diǎn)型等要素,語法形式為:
plt.plot(X, Y, 'format', ...)

點(diǎn)和線的樣式

顏色

  • 參數(shù)color或c
  • 顏色值的方式
    • 合法的HTML顏色名
      • color = 'red'
    • 別名
      • color='r'
    • HTML十六進(jìn)制字符串
      • color = '#eeefff'
    • 歸一化到[0, 1]的RGB元組
      • color = (0.3, 0.3, 0.4)
        顏色 別名 HTML顏色名 顏色 別名 HTML顏色名
        藍(lán)色 b blue 綠色 g green
        紅色 r red 黃色 y yellow
        青色 c cyan 黑色 k black
        洋紅色 m magenta 白色 w white
x = np.linspace(-np.pi,np.pi,100)
x
# 設(shè)置顏色
# plt.plot(x,np.sin(x),color='red')
# plt.plot(x,np.sin(x),c='red')
# plt.plot(x,np.sin(x),c='r')
# plt.plot(x,np.sin(x),c='g')
# plt.plot(x,np.sin(x),c='#aabbcc')
plt.plot(x,np.sin(x),c=(1,0.2,0.5))

透明度

plt.plot() 中的 alpha參數(shù)

x = np.linspace(-np.pi,np.pi,100)
plt.plot(x,np.sin(x),c=(1,0.2,0.5),alpha=0.2)  # 取值范圍0-1

背景色

設(shè)置背景色突想,通過plt.subplot()方法傳入facecolor參數(shù)允蜈,來設(shè)置坐標(biāo)系的背景色

x = np.linspace(-np.pi,np.pi,100)
# plt.plot(x,np.sin(x),c=(1,0,0))  # 注意 facecolor='gray' 不是plt.plot()的參數(shù)
plt.subplot(facecolor='gray')  # 一定要注意 先畫背景色 再劃線 否則背景色就把線給蓋住了
plt.plot(x,np.sin(x),c=(1,0,0))

線型和線寬

  • 參數(shù)linestyle或ls
  • linewidth或lw參數(shù)
線條風(fēng)格 描述 線條風(fēng)格 描述
'-' 實(shí)線 ':' 虛線
'--' 破折線 'steps' 階梯線
'-.' 點(diǎn)劃線 'None' / ',' 什么都不畫
x = np.arange(0,10,1)
x
# linestyle 線的樣式
# plt.plot(x,x,linestyle='-')
# plt.plot(x,x,linestyle=':')  # dotted
# plt.plot(x,x,linestyle='--')  # dashed
# plt.plot(x,x,linestyle='-.')
# plt.plot(x,x,linestyle='steps')
# plt.plot(x,x,linestyle='None')
# plt.plot(x,x,linestyle='--',linewidth=5)
plt.plot(x,x,ls='--',lw=5)  # ls linestyle 線的樣式 lw linewidth 線的寬度

破折線

dashes參數(shù) eg.dashes = [20,50,5,2,10,5]

設(shè)置破折號(hào)序列各段的寬度

x = np.arange(-np.pi,np.pi,0.1)
y = np.sin(x)
# plt.plot(x,y,dashes=[10])  # 必須是偶數(shù)個(gè)值 否則會(huì)報(bào)錯(cuò)
# plt.plot(x,y,dashes=[10,10])  # 實(shí)現(xiàn)長(zhǎng)10 間隙長(zhǎng)10
# plt.plot(x,y,dashes=[10,5,5,2])
# plt.plot(x,y,dashes=[2,4,3,5,10,5])

點(diǎn)型

  • marker 設(shè)置點(diǎn)形
  • markersize 設(shè)置點(diǎn)形大小
標(biāo)記 描述 標(biāo)記 描述
'1' 一角朝下的三腳架 '3' 一角朝左的三腳架
'2' 一角朝上的三腳架 '4' 一角朝右的三腳架
標(biāo)記 描述 標(biāo)記 描述
's' 正方形 'p' 五邊形
'h' 六邊形1 'H' 六邊形2
'8' 八邊形
標(biāo)記 描述 標(biāo)記 描述
'.' 點(diǎn) 'x' X
'*' 星號(hào) '+' 加號(hào)
',' 像素
標(biāo)記 描述 標(biāo)記 描述
'o' 圓圈 'D' 菱形
'd' 小菱形 '','None',' ',None
標(biāo)記 描述 標(biāo)記 描述
'_' 水平線 ' ' 豎線
標(biāo)記 描述 標(biāo)記 描述
'v' 一角朝下的三角形 '<' 一角朝左的三角形
'^' 一角朝上的三角形 '>' 一角朝右的三角形
x = np.arange(1,10,1)
x
y = np.sin(x)
# plt.plot(x,y)
# plt.plot(x,y,marker='1',markersize=20)
# plt.plot(x,y,marker='2',markersize=20)
# plt.plot(x,y,marker='1',markersize=20)
# plt.plot(x,y,marker='h',markersize=20)
# plt.plot(x,y,marker='H',markersize=20)
# plt.plot(x,y,marker='*',markersize=20)
# plt.plot(x,y,marker='|',markersize=20)
# plt.plot(x,y,marker='_',markersize=20)
x = np.arange(1,10,1)
x
y = np.sin(x)
plt.plot(x,y,marker='h',markersize=20,markeredgecolor='red',markeredgewidth=5,markerfacecolor='green')

更多點(diǎn)和線的設(shè)置

  • markeredgecolor = 'green',
  • markeredgewidth = 2,
  • markerfacecolor = 'purple'
參數(shù) 描述 參數(shù) 描述
color或c 線的顏色 linestyle或ls 線型
linewidth或lw 線寬 marker 點(diǎn)型
markeredgecolor 點(diǎn)邊緣的顏色 markeredgewidth 點(diǎn)邊緣的寬度
markerfacecolor 點(diǎn)內(nèi)部的顏色 markersize 點(diǎn)的大小
x = np.arange(1,10,1)
x
y = np.sin(x)
plt.plot(x,y,marker='h',markersize=20,markeredgecolor='red',markeredgewidth=5,markerfacecolor='green')

在一條語句中為多個(gè)曲線進(jìn)行設(shè)置

多個(gè)曲線同一設(shè)置

屬性名聲明蒿柳,不可以多參數(shù)連用

plt.plot(x1, y1, x2, y2, ...)

# 同時(shí)設(shè)置多個(gè)曲線的樣式 (多個(gè)曲線樣式相同)
x = np.linspace(0,100,30)  # 0-100要30個(gè)
x
# plt.plot(x1,y1,x2,y2,x3,y3...) 顏色 點(diǎn)的形狀 線的樣式
plt.plot(x,x,x,2*x,x,x/2,c='r',marker='*',ls=':')

多個(gè)曲線不同設(shè)置

多個(gè)都進(jìn)行設(shè)置時(shí),多參數(shù)連用 plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)

# 同時(shí)設(shè)置多個(gè)曲線的樣式 (多個(gè)曲線樣式不同)
x = np.linspace(0,100,30)  # 0-100要30個(gè)
x
# plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...) fmt format 格式 格式化字符串 就是按照人家指定的格式去設(shè)置樣式的字符串
# fmt = '[color][marker][line]' '顏色 點(diǎn)的樣式 線的樣式'
plt.plot(x,x,'ro:',x,2*x,'k*--',x,x/2,'c1-.')  # 青色 1 -.

三種設(shè)置方式

向方法傳入關(guān)鍵字參數(shù)

plt.plot(...)

就是之前我們一直用的 調(diào)用plt.plot()繪圖的時(shí)候往里面?zhèn)鲄?shù)的方式

優(yōu)點(diǎn):簡(jiǎn)潔方便 缺點(diǎn):容易亂 沒提示 可讀性差

對(duì)坐標(biāo)系使用一系列的setter方法

  • axes = plt.subplot()獲取坐標(biāo)系
    • set_title()
    • set_facecolor()
    • set_xticks漩蟆、set_yticks 設(shè)置刻度值
    • set_xticklabels垒探、set_yticklabels 設(shè)置刻度名稱
x = np.linspace(0,100,30)  # 0-100要30個(gè)
x
axes  = plt.subplot()
axes.plot(x,x,x,2*x,x,x/2)
axes.set_title('title')
axes.set_facecolor('gray')
axes.set_xlabel('x')
axes.set_ylabel('y')

對(duì)實(shí)例使用一系列的setter方法

  • plt.plot()方法返回一個(gè)包含所有線的列表,設(shè)置每一個(gè)線需要獲取該線對(duì)象
    • eg: lines = plt.plot(); line = lines[0]
    • line.set_linewith()
    • line.set_linestyle()
    • line.set_color()
x = np.linspace(0,100,30)  # 0-100要30個(gè)
x
lines = plt.plot(x,x,x,2*x,x,x/2)
lines[0].set_linewidth(5)
lines[1].set_linestyle(':')
lines[2].set_color('y')

X怠李、Y軸坐標(biāo)刻度

plt.xticks()和plt.yticks()方法

  • 需指定刻度值和刻度名稱 plt.xticks([刻度列表],[名稱列表])
  • 支持fontsize圾叼、rotation、color等參數(shù)設(shè)置

正弦余弦

x = np.linspace(-np.pi,np.pi,100)
x
plt.plot(x,np.sin(x))
# plt.xticks([-3,0,3])  # x軸線上的刻度
# plt.xticks([-np.pi,0,np.pi])
# plt.xticks([-np.pi,0,np.pi],['-π',0,'π'])
# plt.yticks([-1,0,1])
plt.xticks(np.arange(-4,5,2))
plt.grid()
# 關(guān)于grid密度的問題
x = np.arange(-np.pi, np.pi, step=0.1)
plt.plot(x,np.sin(x))
# plt.xticks(np.arange(-4,4,0.5))
plt.xticks(np.arange(-4,4,1))
plt.grid()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捺癞,一起剝皮案震驚了整個(gè)濱河市夷蚊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌髓介,老刑警劉巖惕鼓,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異唐础,居然都是意外死亡箱歧,警方通過查閱死者的電腦和手機(jī)矾飞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呀邢,“玉大人洒沦,你說我怎么就攤上這事〖厶剩” “怎么了申眼?”我有些...
    開封第一講書人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蝉衣。 經(jīng)常有香客問我括尸,道長(zhǎng),這世上最難降的妖魔是什么买乃? 我笑而不...
    開封第一講書人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任姻氨,我火速辦了婚禮,結(jié)果婚禮上剪验,老公的妹妹穿的比我還像新娘肴焊。我一直安慰自己,他們只是感情好功戚,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開白布娶眷。 她就那樣靜靜地躺著,像睡著了一般啸臀。 火紅的嫁衣襯著肌膚如雪届宠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,784評(píng)論 1 290
  • 那天乘粒,我揣著相機(jī)與錄音豌注,去河邊找鬼。 笑死灯萍,一個(gè)胖子當(dāng)著我的面吹牛轧铁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播旦棉,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼齿风,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了绑洛?” 一聲冷哼從身側(cè)響起救斑,我...
    開封第一講書人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎真屯,沒想到半個(gè)月后脸候,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年纪他,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鄙煤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡茶袒,死狀恐怖梯刚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情薪寓,我是刑警寧澤亡资,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站向叉,受9級(jí)特大地震影響锥腻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜母谎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一瘦黑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧奇唤,春花似錦幸斥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至懈贺,卻和暖如春经窖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背梭灿。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工画侣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人堡妒。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓棉钧,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親涕蚤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容