我的Python自学之路

Python 学习笔记 095

2020-01-26  本文已影响0人  夜羽萧轩

```python

#import pandas as pd

import matplotlib.pypblot

```

```python

import numpy as np

from matplotlib import pyplot as plt #plt为变量名

x = np.arange(1,120)

y =  2  * x* x +  5

plt.title("Matplotlib demo") #定义图表标题

plt.xlabel("x axis caption") #横轴标签

plt.ylabel("y axis caption") # 纵轴标签

plt.plot(x,y)

#plt.plot(y,x)  #反函数的绘制

plt.show()

```

![png](output_1_0.png)

```python

a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])

for i in a:

    print(i)

#打印支持的字体列表

```

    AR BERKLEY

    AR BERKLEY

    AR BLANCA

    AR BLANCA

    AR BONNIE

    AR BONNIE

    AR CARTER

    AR CARTER

    AR CENA

    AR CENA

    AR CHRISTY

    AR CHRISTY

    AR DARLING

    AR DARLING

    AR DECODE

    AR DECODE

    AR DELANEY

    AR DELANEY

    AR DESTINE

    AR DESTINE

    AR ESSENCE

    AR ESSENCE

    AR HERMANN

    AR HERMANN

    AR JULIAN

    AR JULIAN

    Aharoni

    Aldhabi

    Algerian

    Algerian

    Andalus

    Angsana New

    Angsana New

    Angsana New

    Angsana New

    AngsanaUPC

    AngsanaUPC

    AngsanaUPC

    AngsanaUPC

    Aparajita

    Aparajita

    Aparajita

    Aparajita

    Arabic Typesetting

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Arial

    Baskerville Old Face

    Baskerville Old Face

    Bauhaus 93

    Bauhaus 93

    Bell MT

    Bell MT

    Bell MT

    Bell MT

    Bell MT

    Bell MT

    Berlin Sans FB

    Berlin Sans FB

    Berlin Sans FB

    Berlin Sans FB

    Berlin Sans FB Demi

    Berlin Sans FB Demi

    Bernard MT Condensed

    Bernard MT Condensed

    Bodoni MT

    Bodoni MT

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Book Antiqua

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookman Old Style

    Bookshelf Symbol 7

    Bookshelf Symbol 7

    Britannic Bold

    Britannic Bold

    Broadway

    Broadway

    Browallia New

    Browallia New

    Browallia New

    Browallia New

    BrowalliaUPC

    BrowalliaUPC

    BrowalliaUPC

    BrowalliaUPC

    Brush Script MT

    Brush Script MT

    Calibri

    Calibri

    Calibri

    Calibri

    Calibri

    Calibri

    Californian FB

    Californian FB

    Californian FB

    Californian FB

    Californian FB

    Californian FB

    Cambria

    Cambria

    Cambria

    Candara

    Candara

    Candara

    Candara

    Candara

    Candara

    Candara

    Candara

    Centaur

    Centaur

    Century

    Century

    Century Gothic

    Century Gothic

    Century Gothic

    Century Gothic

    Century Gothic

    Century Gothic

    Century Gothic

    Century Gothic

    Chiller

    Chiller

    Colonna MT

    Colonna MT

    Comic Sans MS

    Comic Sans MS

    Comic Sans MS

    Comic Sans MS

    Consolas

    Consolas

    Consolas

    Consolas

    Constantia

    Constantia

    Constantia

    Constantia

    Cooper Black

    Cooper Black

    Corbel

    Corbel

    Corbel

    Corbel

    Cordia New

    Cordia New

    Cordia New

    Cordia New

    CordiaUPC

    CordiaUPC

    CordiaUPC

    CordiaUPC

    Courier New

    Courier New

    Courier New

    Courier New

    DFKai-SB

    DaunPenh

    David

    David

    DejaVu Sans

    DejaVu Sans

    DejaVu Sans

    DejaVu Sans

    DejaVu Sans Display

    DejaVu Sans Mono

    DejaVu Sans Mono

    DejaVu Sans Mono

    DejaVu Sans Mono

    DejaVu Serif

    DejaVu Serif

    DejaVu Serif

    DejaVu Serif

    DejaVu Serif Display

    DengXian

    DengXian

    DengXian

    DengXian

    DengXian

    DengXian

    DilleniaUPC

    DilleniaUPC

    DilleniaUPC

    DilleniaUPC

    DokChampa

    Ebrima

    Ebrima

    Estrangelo Edessa

    EucrosiaUPC

    EucrosiaUPC

    EucrosiaUPC

    EucrosiaUPC

    Euphemia

    FZShuTi

    FZShuTi

    FZYaoTi

    FZYaoTi

    FangSong

    Footlight MT Light

    Footlight MT Light

    FrankRuehl

    Franklin Gothic Medium

    Franklin Gothic Medium

    FreesiaUPC

    FreesiaUPC

    FreesiaUPC

    FreesiaUPC

    Freestyle Script

    Freestyle Script

    Gabriola

    Gabriola

    Gadugi

    Gadugi

    Garamond

    Garamond

    Garamond

    Garamond

    Garamond

    Garamond

    Gautami

    Gautami

    Georgia

    Georgia

    Georgia

    Georgia

    Gisha

    Gisha

    Harlow Solid Italic

    Harlow Solid Italic

    Harrington

    Harrington

    High Tower Text

    High Tower Text

    High Tower Text

    High Tower Text

    Impact

    Informal Roman

    Informal Roman

    IrisUPC

    IrisUPC

    IrisUPC

    IrisUPC

    Iskoola Pota

    Iskoola Pota

    JasmineUPC

    JasmineUPC

    JasmineUPC

    JasmineUPC

    Javanese Text

    Jokerman

    Jokerman

    Juice ITC

    Juice ITC

    KaiTi

    Kalinga

    Kalinga

    Kartika

    Kartika

    Khmer UI

    Khmer UI

    Khmer UI

    Khmer UI

    KodchiangUPC

    KodchiangUPC

    KodchiangUPC

    KodchiangUPC

    Kokila

    Kokila

    Kokila

    Kokila

    Kristen ITC

    Kristen ITC

    Kunstler Script

    Kunstler Script

    Lao UI

    Lao UI

    Lao UI

    Lao UI

    Latha

    Latha

    Leelawadee

    Leelawadee

    Leelawadee UI

    Leelawadee UI

    Leelawadee UI

    Leelawadee UI

    Leelawadee UI

    Leelawadee UI

    Levenim MT

    Levenim MT

    LiSu

    LiSu

    LilyUPC

    LilyUPC

    LilyUPC

    LilyUPC

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Bright

    Lucida Calligraphy

    Lucida Calligraphy

    Lucida Console

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Fax

    Lucida Handwriting

    Lucida Handwriting

    Lucida Sans Unicode

    MS Reference Sans Serif

    MS Reference Sans Serif

    MS Reference Specialty

    MS Reference Specialty

    MT Extra

    MV Boli

    Magneto

    Magneto

    Malgun Gothic

    Malgun Gothic

    Mangal

    Mangal

    Marlett

    Matura MT Script Capitals

    Matura MT Script Capitals

    Microsoft Himalaya

    Microsoft New Tai Lue

    Microsoft New Tai Lue

    Microsoft PhagsPa

    Microsoft PhagsPa

    Microsoft Sans Serif

    Microsoft Tai Le

    Microsoft Tai Le

    Microsoft Uighur

    Microsoft Uighur

    Microsoft Yi Baiti

    Miriam

    Miriam Fixed

    Mistral

    Mistral

    Modern No. 20

    Modern No. 20

    Mongolian Baiti

    Monotype Corsiva

    Monotype Corsiva

    MoolBoran

    Myanmar Text

    Myanmar Text

    Narkisim

    Niagara Engraved

    Niagara Engraved

    Niagara Solid

    Niagara Solid

    Nirmala UI

    Nirmala UI

    Nirmala UI

    Nirmala UI

    Nirmala UI

    Nirmala UI

    Nyala

    Old English Text MT

    Old English Text MT

    Onyx

    Onyx

    Palatino Linotype

    Palatino Linotype

    Palatino Linotype

    Palatino Linotype

    Parchment

    Parchment

    Plantagenet Cherokee

    Playbill

    Playbill

    Poor Richard

    Poor Richard

    Raavi

    Raavi

    Ravie

    Ravie

    Rod

    STCaiyun

    STCaiyun

    STFangsong

    STFangsong

    STHupo

    STHupo

    STIXGeneral

    STIXGeneral

    STIXGeneral

    STIXGeneral

    STIXNonUnicode

    STIXNonUnicode

    STIXNonUnicode

    STIXNonUnicode

    STIXSizeFiveSym

    STIXSizeFourSym

    STIXSizeFourSym

    STIXSizeOneSym

    STIXSizeOneSym

    STIXSizeThreeSym

    STIXSizeThreeSym

    STIXSizeTwoSym

    STIXSizeTwoSym

    STKaiti

    STKaiti

    STLiti

    STLiti

    STSong

    STSong

    STXihei

    STXihei

    STXingkai

    STXingkai

    STXinwei

    STXinwei

    STZhongsong

    STZhongsong

    Sakkal Majalla

    Sakkal Majalla

    Segoe Print

    Segoe Print

    Segoe Script

    Segoe Script

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI

    Segoe UI Emoji

    Segoe UI Symbol

    Shonar Bangla

    Shonar Bangla

    Shonar Bangla

    Shonar Bangla

    Showcard Gothic

    Showcard Gothic

    Shruti

    Shruti

    SimHei

    SimSun-ExtB

    Simplified Arabic

    Simplified Arabic

    Simplified Arabic Fixed

    Snap ITC

    Snap ITC

    Stencil

    Stencil

    Sylfaen

    Symbol

    Tahoma

    Tahoma

    TeamViewer15

    Tempus Sans ITC

    Tempus Sans ITC

    Times New Roman

    Times New Roman

    Times New Roman

    Times New Roman

    Traditional Arabic

    Traditional Arabic

    Trebuchet MS

    Trebuchet MS

    Trebuchet MS

    Trebuchet MS

    Tunga

    Tunga

    Urdu Typesetting

    Urdu Typesetting

    Urdu Typesetting

    Urdu Typesetting

    Utsaah

    Utsaah

    Utsaah

    Utsaah

    Vani

    Vani

    Vani

    Vani

    Verdana

    Verdana

    Verdana

    Verdana

    Vijaya

    Vijaya

    Viner Hand ITC

    Viner Hand ITC

    Vivaldi

    Vivaldi

    Vladimir Script

    Vladimir Script

    Vrinda

    Vrinda

    Webdings

    Wide Latin

    Wide Latin

    Wingdings

    Wingdings 2

    Wingdings 2

    Wingdings 3

    Wingdings 3

    YouYuan

    YouYuan

    Yu Gothic

    Yu Gothic

    Yu Gothic

    Yu Mincho

    Yu Mincho

    Yu Mincho

    cmb10

    cmex10

    cmmi10

    cmr10

    cmss10

    cmsy10

    cmtt10

Matplotlib 默认情况不支持中文 

我们可以使用系统的字体:   

```python

import numpy as np

from matplotlib import pyplot as plt

import matplotlib

# fname 为 你下载的字体库路径,注意 SimHei.ttf 字体的路径

zhfont1 = matplotlib.font_manager.FontProperties(fname="C:\Windows\Fonts\msyh.ttc")  #微软雅黑

x = np.arange(1,11)

y =  2  * x +  5

plt.title("菜鸟教程 - 测试", fontproperties=zhfont1)

# fontproperties 设置中文显示,fontsize 设置字体大小

plt.xlabel("x 轴", fontproperties=zhfont1,)

plt.ylabel("y 轴", fontproperties=zhfont1)

plt.plot(x,y)

plt.show()

```

![png](output_4_0.png)

中文字体测试

```python

import numpy as np

from matplotlib import pyplot as plt

plt.rcParams['font.family']=['Arial']  #字体使用Arial字体

x = np.arange(1,11)

y =  2  * x +  5

plt.title("Matplotlib demo")

plt.xlabel("x axis caption")

plt.ylabel("y axis caption")

plt.plot(x,y,"ob")  #散点图 属性 为ob

plt.show()

```

![png](output_6_0.png)

散点图测试   

使用系统字体   

```python

import numpy as np

import matplotlib.pyplot as plt

# 计算正弦曲线上点的 x 和 y 坐标

x = np.arange(0,  3  * np.pi,  0.1)

y = np.sin(x)

plt.title("sine wave form") 

# 使用 matplotlib 来绘制点

plt.plot(x, y)

plt.show()

```

![png](output_8_0.png)

绘制正弦波

```python

import numpy as np

import matplotlib.pyplot as plt

# 计算正弦和余弦曲线上的点的 x 和 y 坐标

x = np.arange(0,  3  * np.pi,  0.1)

y_sin = np.sin(x)

y_cos = np.cos(x) 

# 建立 subplot 网格,高为 2,宽为 1 

# 激活第一个 subplot

plt.subplot(2,  1,  1) 

# 绘制第一个图像

plt.plot(x, y_sin)

plt.title('Sine') 

# 将第二个 subplot 激活,并绘制第二个图像

plt.subplot(2,  1,  2)

plt.plot(x, y_cos)

plt.title('Cosine') 

# 展示图像

plt.show()

```

![png](output_10_0.png)

绘制两个图形

```python

from matplotlib import pyplot as plt

x =  [5,8,10]

y =  [12,16,6]

x2 =  [6,9,11]

y2 =  [6,15,7]

plt.bar(x, y, align =  'center')

plt.bar(x2, y2, color =  'g', align =  'center')

plt.title('Bar graph')

plt.ylabel('Y axis')

plt.xlabel('X axis')

plt.show()

```

![png](output_12_0.png)

绘制条形图

```python

import numpy as np

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])

np.histogram(a,bins =  [0,20,40,60,80,100])

hist,bins = np.histogram(a,bins =  [0,20,40,60,80,100]) 

print (hist)

print (bins)

```

    [3 4 5 2 1]

    [  0  20  40  60  80 100]

#### numpy.histogram()

numpy.histogram() 函数是数据的频率分布的图形表示。 水平尺寸相等的矩形对应于类间隔,称为 bin,变量 height 对应于频率。

numpy.histogram()函数将输入数组和 bin 作为两个参数。 bin 数组中的连续元素用作每个 bin 的边界。

```python

from matplotlib import pyplot as plt

import numpy as np 

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])

plt.hist(a, bins =  [0,20,40,60,80,100])

plt.title("histogram")

plt.show()

```

![png](output_16_0.png)

#### plt() 

Matplotlib 可以将直方图的数字表示转换为图形。 

pyplot 子模块的 plt() 函数将包含数据和 bin 数组的数组作为参数,并转换为直方图。

### 测试matplotlib & numpy 库   

np.arange() 函数创建 x 轴上的值。y 轴上的对应值存储在另一个数组对象 y 中。       

这些值使用 matplotlib 软件包的 pyplot 子模块的 plot() 函数绘制。     

图形由 show() 函数显示。   

```python

import pandas as pd

dataset = pd.read_csv('pythontest.csv')

dataset.head(10)

```

<div>

<style scoped>

    .dataframe tbody tr th:only-of-type {

        vertical-align: middle;

    }

    .dataframe tbody tr th {

        vertical-align: top;

    }

    .dataframe thead th {

        text-align: right;

    }

</style>

<table border="1" class="dataframe">

  <thead>

    <tr style="text-align: right;">

      <th></th>

      <th>LotID</th>

      <th>GlassID</th>

      <th>AreaNo</th>

      <th>AreaX</th>

      <th>AreaY</th>

      <th>Iter</th>

      <th>RMSESet</th>

      <th>RMSEFin</th>

      <th>L1T</th>

      <th>L1N</th>

      <th>L1K</th>

      <th>L2T</th>

      <th>L2N</th>

      <th>L2K</th>

      <th>Recipe</th>

      <th>Date</th>

      <th>Time</th>

    </tr>

  </thead>

  <tbody>

    <tr>

      <th>0</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>1</td>

      <td>1140</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.956429</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1010.52</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:29:59</td>

    </tr>

    <tr>

      <th>1</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>2</td>

      <td>960</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.845970</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1008.73</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:04</td>

    </tr>

    <tr>

      <th>2</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>3</td>

      <td>790</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>1.082690</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1044.63</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:09</td>

    </tr>

    <tr>

      <th>3</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>4</td>

      <td>620</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.991395</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1069.88</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:14</td>

    </tr>

    <tr>

      <th>4</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>5</td>

      <td>430</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.767140</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1049.54</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:19</td>

    </tr>

    <tr>

      <th>5</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>6</td>

      <td>260</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.798345</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1042.95</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:24</td>

    </tr>

    <tr>

      <th>6</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>7</td>

      <td>90</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>1.001940</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1081.89</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:30</td>

    </tr>

    <tr>

      <th>7</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>8</td>

      <td>-90</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.985975</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1093.59</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:35</td>

    </tr>

    <tr>

      <th>8</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>9</td>

      <td>-260</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.813240</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1037.44</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:40</td>

    </tr>

    <tr>

      <th>9</th>

      <td>5CCBDX0197M</td>

      <td>5ACCD90004A6</td>

      <td>10</td>

      <td>-430</td>

      <td>954</td>

      <td>20</td>

      <td>0.5</td>

      <td>0.860456</td>

      <td>550.0</td>

      <td>1.8723</td>

      <td>0.0224</td>

      <td>1057.37</td>

      <td>1.704</td>

      <td>0.0046</td>

      <td>RC_6514_10.2_PI_126P</td>

      <td>2019/06/15</td>

      <td>14:30:45</td>

    </tr>

  </tbody>

</table>

</div>

```python

dataset.L2T.head(10)

y = dataset.L2T

print(y.head(10),dataset.L2T.head(10))  #普通数组也有head属性

```

    0    1010.52

    1    1008.73

    2    1044.63

    3    1069.88

    4    1049.54

    5    1042.95

    6    1081.89

    7    1093.59

    8    1037.44

    9    1057.37

    Name: L2T, dtype: float64 0    1010.52

    1    1008.73

    2    1044.63

    3    1069.88

    4    1049.54

    5    1042.95

    6    1081.89

    7    1093.59

    8    1037.44

    9    1057.37

    Name: L2T, dtype: float64

#### pause

#### Closed

上一篇下一篇

猜你喜欢

热点阅读