python:matplotlib基础(2)

#%% md

### 图片灰度处理

#%%

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

#%% md

三种方法

#%%

j = plt.imread('jizhengen.png')
plt.imshow(j)

#%%

j.shape

#%%

n = np.array([[1,2,3],[4,5,6]])

#%%

n.mean(1)

#%%

# 使用平均值
j_mean = j.mean(axis=2)

#%%

j_mean.shape

#%%

plt.figure(figsize=(12,9))
plt.imshow(j_mean, cmap='gray')

#%%

# 最大值
j_max = j.max(-1)

#%%

j_max.shape

#%%

plt.figure(figsize=(12,9))
plt.imshow(j_max, cmap='gray')

#%%

# 加权平均值
plt.figure(figsize=(12,9))
j_weight = np.dot(j, [0.299,0.587, 0.114])
plt.imshow(j_weight, cmap='gray')

#%% md

# matplotlib

#%% md

## 目录
+ 一、【重点】Matplotlib基础知识

+ 二、设置plot的风格和样式
    + 1、【重点】点和线的样式
    + 2、X、Y轴坐标刻度


+ 三、2D图形
    + 1、示例
    + 2、【重点】直方图
    + 3、【重点】条形图
    + 4、【重点】饼图
    + 5、【重点】散点图
    
=============以上为重点=================

#### 下面的自学
+ 四、图形内的文字、注释、箭头
    + 1、图形内的文字
    + 2、注释
    + 3、箭头
    
+ 五、3D图
    + 1、曲面图

#%% md

## 一、Matplotlib基础知识

#%% md

Matplotlib中的基本图表包括的元素
+ x轴和y轴  
水平和垂直的轴线


+ x轴和y轴刻度  
刻度标示坐标轴的分隔,包括最小刻度和最大刻度


+ x轴和y轴刻度标签  
表示特定坐标轴的值


+ 绘图区域  
实际绘图的区域

#%%

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

#%%

#pandas 绘图,提供方便
#matplotlib专门绘图工具,样式更加丰富

#%% md

### 只含单一曲线的图

#%%

n  = np.arange(10)
plt.plot(n)

#%%

x = np.linspace(-1, 1, 1000)
y = (1-x**2)**0.5
plt.plot(x,y)

#%%

x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.plot(x,y)

#%% md

### 包含多个曲线的图

#%% md

1、可以使用多个plot函数(推荐),在一个图中绘制多个曲线

#%%

x = np.linspace(-5,5,1000)
y = x*3
plt.plot(x,y)
plt.plot(x,(1-x**2)**0.5)
plt.show()
plt.plot(x,np.cos(x))

#%% md

2、也可以在一个plot函数中传入多对X,Y值,在一个图中绘制多个曲线

#%%

x = np.linspace(-5,5,1000)
y = x*3
plt.plot(x,y,x,(1-x**2)**0.5, x, np.cos(x))

#%% md

### 网格线
绘制正选余弦

#%%

x = np.linspace(-10,10, 1000)
y = np.cos(x)
plt.plot(x,y)
plt.grid(b=True, axis='y', color='red', linestyle='--', linewidth=5)

#%% md

使用plt.grid(True)方法为图添加网格线

#%%

 

#%% md

设置grid参数(参数与plot函数相同),使用plt面向对象的方法,创建多个子图显示不同网格线
![](1.PNG)

- lw代表linewidth,线的粗细
- alpha表示线的明暗程度
- color代表颜色


#%%

figure = plt.figure(figsize=(12,5))
axes1 = figure.add_subplot(131)
axes1.plot(x,y)
axes1.grid(True, color='gray', lw=2)

axes2 = figure.add_subplot(132)
axes2.plot(x,y)
axes2.grid(True, color='r', lw=1)

axes3 = figure.add_subplot(133)
axes3.plot(x, x**2)
axes3.grid(True, color='green', linestyle='--')

#%% md

### 坐标轴界限

#%% md

#### axis方法
如果axis方法没有任何参数,则返回当前坐标轴的上下限axis(xmin =,ymax = )

#%%

plt.plot(x,y)
plt.axis([-10,8,-1,2])

#%%

plt.plot(x,y)
plt.axis('tight')

#%%

plt.plot(x,y)
plt.axis('off')

#%%

plt.plot(x,y)
plt.axis('equal')

#%% md

##### plt.axis('xxx') 'tight'、'off'、'equal'……

设置坐标轴类型  
关闭坐标轴
![](2.png)

#%%

x = np.linspace(-1,1, 1000)
y = (1-x**2)**0.5
# plt.figure(figsize=(4,4))
plt.plot(x,y,x,-y)
plt.axis('equal')

#%%

 

#%% md

#### xlim方法和ylim方法

除了plt.axis方法,还可以通过xlim,ylim方法设置坐标轴范围

#%%

x = np.linspace(-1,1, 1000)
y = (1-x**2)**0.5
# plt.figure(figsize=(4,4))
plt.plot(x,y,x,-y)
plt.xlim(0, 2)
plt.ylim(0, 2)

#%% md

### 坐标轴标签
xlabel方法和ylabel方法  
plt.ylabel('y = x^2 + 5',rotation = 60)旋转

#%%

plt.plot(x,y)
xlabel = plt.xlabel('half circle', fontsize=20)
xlabel.set_bbox(dict(facecolor='red'))
ylabel = plt.ylabel('f(x)=(1-x**2)**0.5')
# ylabel.set_rotation(60)
ylabel.set_position((0,1))

#%% md

### 标题
title方法

#%%

plt.plot(x,y)
title = plt.title('This is a half circle',bbox=dict(facecolor='red'))
title.set_backgroundcolor('green')
title.set_color('cyan')
title.set_animated(b=True)
title.set_rotation(60)

#%% md

### 图例

#%% md

#### legend方法

两种传参方法:
- 【推荐使用】在plot函数中增加label参数
- 在legend方法中传入字符串列表
![](3.png)


#%%

plt.plot(x,y)
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))
plt.legend(['half circle', 'sin', 'cos'], loc=8)

#%%

plt.plot(x,y, label='half circle')
plt.plot(x,np.sin(x), label='sin')
plt.plot(x,np.cos(x), label='cos')
# 需要调用一下legend函数,才能把图例显示
plt.legend(loc=2)

#%% md

label参数为'_xxx',则图例中不显示  
plt.plot(x, x*1.5, label = '_xxx')

#%% md

### loc参数

#%% md

| 字符串       | 数值      | 字符串   |  数值 |
| :-------------: |:-----------:| :-----:|  :-----:|
| 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        |

#%%

plt.plot(x,y, label='half circle')
plt.plot(x,np.sin(x), label='sin')
plt.plot(x,np.cos(x), label='cos')
# 需要调用一下legend函数,才能把图例显示
plt.legend(loc=(1,1))

#%% md

loc参数可以是2元素的元组,表示图例左下角的坐标

#%% md

图例也可以超过图的界限loc = (-0.1,0.9)

#%% md

### ncol参数 columns
ncol控制图例中有几列

#%%

plt.plot(x,y, label='half circle')
plt.plot(x,np.sin(x), label='sin')
plt.plot(x,np.cos(x), label='cos')
# 需要调用一下legend函数,才能把图例显示
plt.legend(loc=(0,1), ncol=3)

#%% md

### linestyle、color、marker
修改线条样式  
![](4.png)

#%%

x1 = np.random.normal(loc=0, scale=6,size=100)*10
x2 = np.random.normal(loc=10, scale=3,size=100)*10
x3 = np.random.normal(loc=20, scale=6,size=100)*10
plt.plot(x1, linestyle='-.', label='line1', color='r', linewidth=3, marker='o',markersize=10)
plt.plot(x2, ls = '--', label='line2', color='b', lw=2, marker='>')
plt.plot(x3, ls=':', label='line3', color='g',   lw=1, marker='d',markersize=10)
plt.legend(loc=(0,1), ncol=3)

#%% md

### 保存图片

#%% md

figure.savefig的选项
+ filename  
含有文件路径的字符串或Python的文件型对象。图像格式由文件扩展名推断得出,例如,.pdf推断出PDF,.png推断出PNG
(“png”、“pdf”、“svg”、“ps”、“eps”……)
+ dpi  
图像分辨率(每英寸点数),默认为100
+ facecolor  
图像的背景色,默认为“w”(白色)

#%%

# 获取figure对戏
figure = plt.figure()
# figure.set_edgecolor('red')
axes = figure.add_subplot(111)
axes.plot(x1, linestyle='-.', label='line1', color='r', linewidth=3, marker='o',markersize=10)
axes.plot(x2, ls = '--', label='line2', color='b', lw=2, marker='>')
axes.plot(x3, ls=':', label='line3', color='g',   lw=1, marker='d',markersize=10)
axes.legend(ncol=3, bbox_to_anchor=[0,1,1,0])
figure.savefig('./save/pic1.eps',dpi=500, facecolor='green', edgecolor='red')

#%% md

## 二、设置plot的风格和样式
plot语句中支持除X,Y以外的参数,以字符串形式存在,来控制颜色、线型、点型等要素,语法形式为:  
plt.plot(X, Y, 'format', ...)

#%%

plt.plot(x,y,color='r')

#%% md

### 点和线的样式

#%% md

#### 颜色
参数color或c

#%% md

##### 颜色值的方式
+ 别名
    + color='r'
    
    
    
+ 合法的HTML颜色名
    + color = 'red'

| 颜色       | 别名      | HTML颜色名  | 颜色   |  别名 |HTML颜色名|
| :-------------: |:---------:|:-----------:| :------:|  :-----:| :-----:|
| 蓝色        | b       | blue      | 绿色   |  g   |  green  |
| 红色        | r       | red      | 黄色    |  y   |  yellow |
| 青色        | c       | cyan      | 黑色   |  k   |  black  |
| 洋红色      | m        | magenta    | 白色   |  w   |  white  |

 

 


+ HTML十六进制字符串
    + color = '#eeefff'       

 


+ 归一化到[0, 1]的RGB元组
    + color = (0.3, 0.3, 0.4)

 

#%%

# plt.plot(x,y,c='#000000')
plt.plot(x,y,c=(1,1,0))

#%% md

##### 透明度
alpha参数

#%%

# 最后只在0到1之间指定
plt.plot(x,y,alpha=1)

#%% md

##### 背景色
设置背景色,通过plt.subplot()方法传入facecolor参数,来设置坐标轴的背景色

#%%

line = plt.plot(x,y)[0]
plt.subplot(111, facecolor='r')
line, = plt.plot(x,y)
type(line)

#%%

figure = plt.figure()
axes = figure.add_subplot(111, facecolor='g')
axes.plot(x,y)

#%% md

#### 线型
参数linestyle或ls

#%% md

| 线条风格     | 描述      | 线条风格 |  描述 |
| :-------------: |:------------:| :----:|  :-----:|
| '-'        | 实线     | ':'     |  虚线 |
| '--'       | 破折线    | 'steps'  |  阶梯线 |
| '-.'       | 点划线    | 'None' / ',' |  什么都不画 |

#%%

x = np.linspace(-1,1,1000)
y = (1-x**2)**0.5
plt.plot(x,y, ls='-.')

#%% md

##### 线宽
linewidth或lw参数

#%%

plt.plot(x,y, lw=5,ls='-.')

#%% md

##### 不同宽度的破折线
dashes参数  
设置破折号序列各段的宽度  
![](5.png)

#%%

x = np.linspace(-1,1,1000)
y = (1-x**2)**0.5
plt.figure(figsize=(12,9))
plt.plot(x,y, dashes=[5,10,1,2], c='r')

#%% md

#### 点型
marker参数

#%% md

| 标记        | 描述       | 标记   |  描述 |
| :-------------: |:-----------:| :----:|  :-----:|
| '1'         | 一角朝下的三脚架      | '3'     |  一角朝左的三脚架 |
| '2'         | 一角朝上的三脚架      | '4'     |  一角朝右的三脚架 |

#%%

plt.plot(x,y, marker=1)

#%% md

| 标记        | 描述       | 标记   |  描述 |
| :-------------: |:-----------:| :----:|  :-----:|
| 's'         | 正方形   | 'p'   | 五边形     |
| 'h'         | 六边形1    | 'H'     | 六边形2    |
| '8'         | 八边形     |

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y, marker='s' ,markersize=20)

#%% md

| 标记        | 描述       | 标记   |  描述 |
| :-------------: |:-----------:| :----:|  :-----:|
| '.'     |  点 | 'x'   | X   |
| '\*'    |  星号  | '+'         | 加号       |
| ','         | 像素       |

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y, marker='*' ,markersize=20)

#%% md

| 标记        | 描述       | 标记   |  描述 |
| :-------------: |:-----------:| :----:|  :-----:|
| 'o'         | 圆圈      | 'D'         | 菱形      |
| 'd'    |  小菱形  |'','None',' ',None| 无       |

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y, marker='D' ,markersize=20)

#%% md

| 标记     | 描述     | 标记   |  描述 |
| :--------: |:----------:| :------:| :----:|
| '\_'     |  水平线    | '|'     |  水平线   
![](6.png)

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y, marker='_' ,markersize=20)

#%% md

| 标记        | 描述       | 标记   |  描述 |
| :-------------: |:-----------:| :----:|  :-----:|
| 'v'         | 一角朝下的三角形 | '<'     |  一角朝左的三角形 |
| '^'         | 一角朝上的三角形 | '>'     |  一角朝右的三角形 |

#%% md

#### 多参数连用
颜色、点型、线型

#%% md

#### 更多点和线的设置

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y,color='r', marker='D' ,markersize=20, ls='--')

#%% md

| 参数        | 描述       | 参数       |  描述   |
| :-------------: |:-----------:| :-------------:|  :------:|
| color或c      | 线的颜色   | linestyle或ls  |  线型   |
| linewidth或lw   | 线宽     | marker       |  点型  |
| markeredgecolor  | 点边缘的颜色 | markeredgewidth | 点边缘的宽度 |
| markerfacecolor  | 点内部的颜色 | markersize    |  点的大小    |  
![](7.png)

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y, marker='o' ,markersize=20, ls='--', color='r', markeredgecolor='g', markeredgewidth=5, lw=3)

#%% md

#### 在一条语句中为多个曲线进行设置

#%% md

##### 多个曲线同一设置
属性名声明  

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

#%%

x = np.linspace(-np.pi,np.pi,10)
plt.plot(x,x*3, x,np.sin(x),x,np.cos(x), ls='--', marker='d')

#%% md

##### 多个曲线不同设置
多个都进行设置时,无需声明属性
plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)

#%%

x = np.linspace(-np.pi,np.pi,10)
plt.figure(figsize=(12,9))
plt.plot(x,x*3, 'r','o','--', x,np.sin(x),'g','d','-.',x,np.cos(x), ls=':', marker='h')

#%% md

#### 三种设置方式

#%% md

##### 向方法传入关键字参数

#%%

x= np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
plt.figure(figsize=(12,9))
plt.plot(x,y,color='r')

#%% md

##### 对实例使用一系列的setter方法

#%%

line, = plt.plot(x,y)
line.set_color('g')

#%%

line.set_color('g')

#%% md

##### 使用setp()方法

#%%

# set propeties
line, = plt.plot(x,y)
plt.setp(line, ls='--')

#%% md

### X、Y轴坐标刻度
xticks()和yticks()方法  
![](8.png)

#%%

line, = plt.plot(x,y)
plt.xticks([-np.pi, -np.pi/2,0, np.pi/2, np.pi], ['-pi', '-pi/2', '0', 'pi/2', 'pi'])
plt.yticks([-1,0,1], ['min', '0','max'])

#%% md

#### 面向对象方法
set_xticks、set_yticks、set_xticklabels、set_yticklabels方法

#%%

figure= plt.figure()
axes = figure.add_subplot(111)
axes.set_xticks([-np.pi, -np.pi/2,0, np.pi/2, np.pi])
axes.set_xticklabels(['-pi', '-pi/2', '0', 'pi/2', 'pi'], fontsize=20, rotation=60, color='r')
axes.plot(x,y)


#%% md

#### 正弦余弦
LaTex语法,用$\pi$等表达式在图表上写上希腊字母  
![](9.png)

#%%

figure= plt.figure()
axes = figure.add_subplot(111)
axes.set_xticks([-np.pi, -np.pi/2,0, np.pi/2, np.pi])
axes.set_xticklabels(['$-\delta$', '-$\pi$/2', '0', '$\pi$/2', '$\pi$'], fontsize=20, rotation=60, color='r')
axes.plot(x,y)


#%% md

## 三、2D图形

#%% md

### 直方图

【直方图的参数只有一个x!!!不像条形图需要传入x,y】

hist()的参数
+ bins  
可以是一个bin数量的整数值,也可以是表示bin的一个序列。默认值为10
+ normed  
如果值为True,直方图的值将进行归一化处理,形成概率密度,默认值为False
+ color  
指定直方图的颜色。可以是单一颜色值或颜色的序列。如果指定了多个数据集合,颜色序列将会设置为相同的顺序。如果未指定,将会使用一个默认的线条颜色
+ orientation  
通过设置orientation为horizontal创建水平直方图。默认值为vertical

#%%

 

#%% md

### 条形图

【条形图有两个参数x,y!】

bar()、barh()

#%%

 

#%% md

#### 水平条形图
barh()

#%% md

### 饼图

【饼图也只有一个参数x!】

pie()  
饼图适合展示各部分占总体的比例,条形图适合比较各部分的大小

#%%

n = [3,4,5]
# n = [0.3,0.4,0.2]
plt.figure(figsize=(8,8))
plt.pie(n, labels=['red','green','blue'], labeldistance=0.5, explode=[0.1,0.1,0], colors=['red','green','blue'], autopct='%0.2f%%',
        pctdistance=1.1,startangle=60, shadow=True)

#%% md

普通各部分占满饼图

#%% md

普通未占满饼图

#%% md

饼图阴影、分裂等属性设置
#labels参数设置每一块的标签;labeldistance参数设置标签距离圆心的距离(比例值)
#autopct参数设置比例值的显示格式(%1.1f%%);pctdistance参数设置比例值文字距离圆心的距离
#explode参数设置每一块顶点距圆形的长度(比例值);colors参数设置每一块的颜色;
#shadow参数为布尔值,设置是否绘制阴影

#%% md

startangle设置旋转角度

#%% md

### 散点图

【散点图需要两个参数x,y,但此时x不是表示x轴的刻度,而是每个点的横坐标!】

scatter()  
![](10.png)

#%%

x = np.random.randn(1000)
y = np.random.randn(1000)

size = np.random.randint(1,100,size=1000)
colors = np.random.rand(3000).reshape((1000,3))
plt.figure(figsize=(12,9))
# 散点图
plt.scatter(x,y, c=colors, marker='d', s=size)


#%%

 

#%% md

## <font color = red>四、图形内的文字、注释、箭头(自学)</font>

#%% md

控制文字属性的方法:  


| Pyplot函数     | API方法                | 描述                     |
| :-------------: |:------------------------------:| :---------------------------------:|
| text()       |  mpl.axes.Axes.text()       | 在Axes对象的任意位置添加文字     |
| xlabel()      |  mpl.axes.Axes.set_xlabel()   | 为X轴添加标签               |
| ylabel()      |  mpl.axes.Axes.set_ylabel()   | 为Y轴添加标签               |
| title()       | mpl.axes.Axes.set_title()   |  为Axes对象添加标题            |
| legend()      | mpl.axes.Axes.legend()      |  为Axes对象添加图例            |
| figtext()     |  mpl.figure.Figure.text()    |  在Figure对象的任意位置添加文字    |
| suptitle()     | mpl.figure.Figure.suptitle() |  为Figure对象添加中心化的标题     |
| annnotate()    |  mpl.axes.Axes.annotate()    |  为Axes对象添加注释(箭头可选)    |  


所有的方法会返回一个matplotlib.text.Text对象

#%% md

### 图形内的文字
text()  
![](14.png)

#%%

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
plt.text(0.2,0, 'sin(0)=0')
plt.text(np.pi,0, 'sin($\pi$)=0')
plt.figtext(0.65,0.15, 'sin($3\pi/2$)=-1')

#%% md

使用figtext()

#%% md

### 注释
annotate()  
xy参数设置箭头指示的位置,xytext参数设置注释文字的位置  
arrowprops参数以字典的形式设置箭头的样式  
width参数设置箭头长方形部分的宽度,headlength参数设置箭头尖端的长度,  
headwidth参数设置箭头尖端底部的宽度,  
facecolor设置箭头颜色  
shrink参数设置箭头顶点、尾部与指示点、注释文字的距离(比例值)  
![](15.png)

#%%

x = np.array([14,11,13,12,13,10,30,12,11,13,12,12,11,12])
plt.plot(x)
# 把y轴拉长
plt.ylim(ymax=40)
# 注解
#  width        the width of the arrow in points
#     headwidth    the width of the base of the arrow head in points
#     headlength   the length of the arrow head in points
#     shrink       fraction of total length to 'shrink' from both ends
plt.annotate('this spot must really mean something', (6,30),(8,33), arrowprops=dict(width=10, headwidth=15,headlength=10, shrink=0.2,facecolor='black'
                                                                                   ))


#%% md

练习  
三个随机正太分布数据
![](16.png)  
设置图例  
bbox_to_anchor,ncol mode,borderaxespad  
设置注解 arrowstyle

#%%

x1 = np.random.normal(loc=30, scale=2, size=100)
x2 = np.random.normal(loc=20, scale=2, size=100)
x3 = np.random.normal(loc=10, scale=3, size=100)
plt.plot(x1)
plt.plot(x2)
plt.plot(x3)
plt.ylim(ymax=40)
# 图例
plt.legend(['line1', 'line2','line3'], bbox_to_anchor=[0,1,1,0],ncol=3, mode='expand',borderaxespad=0)
# 注释
plt.annotate('important value', (65,20),(25,35), arrowprops=dict(arrowstyle='->'))

#%% md

### 箭头
箭头的样式,没必要记

#%%

plt.figure(figsize=(12,9))
plt.axis([0, 10, 0, 20]);
arrstyles = ['-', '->', '-[', '<-', '<->', 'fancy', 'simple', 'wedge']
for i, style in enumerate(arrstyles):
    plt.annotate(style, xytext=(1, 2+2*i), xy=(4, 1+2*i), arrowprops=dict(arrowstyle=style));

connstyles=["arc", "arc,angleA=10,armA=30,rad=30", "arc3,rad=.2", "arc3,rad=-.2", "angle", "angle3"]
for i, style in enumerate(connstyles):
    plt.annotate(style, xytext=(6, 2+2*i), xy=(8, 1+2*i), arrowprops=dict(arrowstyle='->', connectionstyle=style));
plt.show()

#%% md

## <font color = red>五、3D图(自学)</font>

#%% md

#### 曲面图  
![](11.png)

#%% md

导包  
from mpl_toolkits.mplot3d.axes3d import Axes3D

#%%

from mpl_toolkits.mplot3d.axes3d import Axes3D

#%% md

生成数据

#%%

display(X,Y)

#%%

x = np.linspace(0,2*np.pi,100)
y = np.linspace(0,2*np.pi,100)

# 使用x,y生成一个面.
X,Y = np.meshgrid(x,y)
# plt.plot(X,Y)
# 生成Z轴
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(12,9))
# 生成3D的坐标轴.
axes1 = plt.subplot(121, projection='3d')
# 把Z在3D坐标轴中画出来.
axes1.plot_surface(X,Y,Z, rstride = 5,cstride = 5)

axes1 = plt.subplot(122, projection='3d')
# 把Z在3D坐标轴中画出来.
p = axes1.plot_surface(X,Y,Z, cmap='rainbow')
plt.colorbar(p, shrink=0.5)

#%%

n1 = [1,2,3]
n2 = [4,5,6,7]
X,Y = np.meshgrid(n1,n2)
display(X,Y)

#%% md

绘制图形

#%%

 

#%% md

## <font color = red>玫瑰图/极坐标条形图(自学)</font>  
![](13.png)

#%%

x = np.random.randint(0,10,size=10)
y = np.random.randint(20,30,size=10)
display(x,y)
plt.bar(x,y, width=0.5)

#%%

wind = np.load('Ravenna_wind.npy')
wind

#%%

# 划分数据
degree, ranges = np.histogram(wind, bins=8, range=[0,360])

#%%

display(degree, ranges)

#%% md

创建极坐标条形图

#%%

plt.axes([0.,0.,1.5,1.5], polar=True, facecolor='g')
ranges = np.arange(0,2*np.pi,2*np.pi/8)
colors = np.random.rand(8,3)
plt.bar(ranges, degree, color=colors,width = 2*np.pi/8)
plt.title('Revenna')

#%% md

使用np.histogram计算一组数据的直方图

#%%

ranges = np.arange(0,2*np.pi,2*np.pi/8)


#%%

plt.bar(ranges, degree, width = 2*np.pi/8)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值