Matplotlib

Matplotlib

import numpy as np
import matplotlib.pyplot as plt

目录

  • 一、【重点】Matplotlib基础知识

  • 二、设置plot的风格和样式

    • 1、【重点】点和线的样式
    • 2、X、Y轴坐标刻度
  • 三、2D图形

    • 1、示例
    • 2、【重点】直方图
    • 3、【重点】条形图
    • 4、【重点】饼图
    • 5、【重点】散点图
  • 四、图形内的文字、注释、箭头

    • 1、图形内的文字
    • 2、注释
    • 3、箭头
  • 五、3D图

    • 1、曲面图

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

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

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

  • 绘图区域
    实际绘图的区域

只含单一曲线的图

x = np.linspace(-5,5,100)
y = np.sin(x)

plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x866a4a8>]

这里写图片描述

包含多个曲线的图

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

y2 = np.cos(x)
#条用多个plot,多个函数就被绘制都一个图中了
plt.plot(x,y)
plt.plot(x,y2)
[<matplotlib.lines.Line2D at 0x8c663c8>]

png

#plot x and y using blue circle markers  ,绘制成小圆点
#plt.plot(y,'g>')
#plt.plot(y,'g+')
plt.plot(y,'bo')
[<matplotlib.lines.Line2D at 0x10a599a20>]

png

网格线

绘制正玄余弦

plt.plot(x,y,x,y2)

#grid [ɡrɪd] 格子
plt.grid(True)

png

设置grid参数(参数与plot函数相同),使用plt面向对象的方法,创建多个子图显示不同网格线
这里写图片描述

  • lw代表linewidth,线的粗细
  • alpha表示线的明暗程度
  • color代表颜色
# 画三张网格线,并且都放在一行
# figsize=(12,6) 第一个值是x, 第二个是y
# plt.figure(figsize=(12,6))

# 第一张
axes = plt.subplot(1, 3, 1)
axes.plot(x, y)
axes.grid(color='pink')

# 第二张
axes = plt.subplot(1, 3, 2)
axes.plot(x, y)
axes.grid(color='orange',linestyle = '-.',axis='x')

# 第三张
axes = plt.subplot(1, 3, 3)
axes.plot(x, y)
axes.grid(color='orange',linestyle = 'solid',axis='y', alpha=0.1)

png

坐标轴界限

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

plt.plot(x, y, x, y2)
# 如果要自己修改刻度界限,那么值必须是序列,前两个代表X轴的范围,后两个代表y轴的范围,必须要成对出现
# plt.axis([-8,8,-6,6])
# 取消坐标轴刻度
# plt.axis('off')
# 缩放
# plt.axis('scaled')
# 缩略图
# plt.axis('tight')
# 自动 与原图没区别
# plt.axis('auto')
# 相等的 主要是用于圆形的绘制
# plt.axis('equal')
# 缩放
# plt.axis('image')
# 正常的
# plt.axis('normal')
  • 输出

    (-5.5, 5.5, -1.0999854706024779, 1.0999854706024779)

png

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

# 改变轴
plt.axis('equal')
  • 输出

    (-1.1, 1.1, -1.0999994488982097, 1.0999994488982097)

png

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

plt.plot(x, f(x),x, -f(x))

# 改变轴
plt.axis('equal')
# xlim,ylim 他们的值必须是一个序列
plt.xlim([-3, 2])

plt.ylim([-3, 2])

# 给轴设置标题
# xlabel,ylabel
# 第一个值s -> str
plt.xlabel('X',fontsize=20, color='red')
plt.ylabel('f(x) = (1 - x^2)^0.5',fontsize=15,rotation=90,color='blue')
  • 输出

    Text(0,0.5,’f(x) = (1 - x^2)^0.5’)

png

标题的设置

title方法

plt.plot(x, f(x),x, -f(x))

# 改变轴
plt.axis('equal')
# xlim,ylim 他们的值必须是一个序列
plt.xlim([-3, 2])

plt.ylim([-3, 2])

# 给轴设置标题
# xlabel,ylabel
# 第一个值s -> str
plt.xlabel('X',fontsize=20, color='red')
plt.ylabel('f(x) = (1 - x^2)^0.5',fontsize=15,rotation=90,color='blue')

# 设置标题
plt.title('this is circle',fontsize=20,color='red', rotation=0)
  • 输出

    Text(0.5,1,’this is circle’)

png

图例

legend方法

两种传参方法:

  • 【推荐使用】在plot函数中增加abel参数
  • 在legend方法中传入字符串列表
x = np.linspace(-np.pi, np.e, 100)
y = np.sin(x)
z = np.cos(x)

plt.plot(x, y, x, z)
# Series和DataFrame会自动显示图例,因为它们都有标签
# legend它的值是一个序列
plt.legend(['sin(x)','cos(x)'])
<matplotlib.legend.Legend at 0xb0dfc18>

png

我们使用label属性

plt.plot(x, y, label='sin(x)')
plt.plot(x, z, label='cos(x)')
# label属性的作用就好比是给legend赋值的,但是没有触发legend函数
plt.legend()
<matplotlib.legend.Legend at 0xab89d30>

png

plot中,label的值前面加上一个下划线就不显示,表示隐藏

res = input('显示sin或者cos:')
  • 输出
    显示sin或者cos:sin
if res == 'sin':
    plt.plot(x, y, label='sin(x)')
    plt.plot(x, z, label='_cos(x)')
else:
    plt.plot(x, y, label='_sin(x)')
    plt.plot(x, z, label='cos(x)')


# label属性的作用就好比是给legend赋值的,但是没有触发legend函数
plt.legend()
<matplotlib.legend.Legend at 0xb2cc978>

png

loc参数

字符串数值字符串数值
best0center left6
upper right1center right7
upper left2lower center8
lower left3upper center9
lower right4center10
right5

loc 是控制图例的位置的

best 是最好的位置

default :upper right 右上角

plt.plot(x, y, label='sin(x)')
plt.plot(x, z, label='cos(x)')
# loc可以是一个序列,和坐标无关,以左下角为基准
plt.legend(loc = 1)
<matplotlib.legend.Legend at 0xf82ce80>

png

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

plt.plot(x, y, label='sin(x)')
plt.plot(x, z, label='cos(x)')
# loc可以是一个序列,和坐标无关,以左下角为基准
plt.legend(loc = [1, 1])
<matplotlib.legend.Legend at 0xf89f7f0>

png

图例也可以超过图的界限

ncol参数

ncol控制图例中有几列

# ncol n + columns
plt.plot(x, y, label='sin(x)')
plt.plot(x, z, label='cos(x)')
# loc可以是一个序列,和坐标无关,以左下角为基准
plt.legend(ncol=2)
<matplotlib.legend.Legend at 0xfb09cf8>

png

linestyle、color、marker

修改线条样式
这里写图片描述

x = np.linspace(-4, 4, 100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x, y, label='sin(x)', linestyle='-.', color='pink',marker='o')
plt.plot(x, z, label='cos(x)',linewidth=10)
plt.plot(x, y * 2 + z / 2, label='complex',linestyle=':')
plt.legend()
<matplotlib.legend.Legend at 0xfdcdd68>

png

保存图片

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

x = np.linspace(-4, 4, 100)
y = np.sin(x)
z = np.cos(x)
plt.plot(x, y, label='sin(x)', linestyle='-.', color='pink',marker='o')
plt.plot(x, z, label='cos(x)',linewidth=10)
plt.plot(x, y * 2 + z / 2, label='complex',linestyle=':')
plt.legend()

plt.savefig('./fig.png',dpi=100, facecolor='orange')

png

# facecolor 代表背景色,但是在图片存储的时候不会被保存下来
figure = plt.figure(facecolor='purple')
axes = figure.add_subplot(1,1,1)
axes.plot(x, y, label='sin(x)', linestyle='-.', color='pink',marker='o')
axes.plot(x, z, label='cos(x)',linewidth=10)
# axes.plot(x, y * 2 + z / 2, label='complex',linestyle=':')

axes.legend()
# savafig保存图片是plt类对象的方法,并不是某个实例的方法
plt.savefig('./fig_.png', dpi=1000)

png

二、设置plot的风格和样式

plot语句中支持除X,Y以外的参数,以字符串形式存在,来控制颜色、线型、点型等要素,语法形式为:
plt.plot(X, Y, ‘format’, …)

x = np.linspace(-4, 4, 10)
y = np.sin(x)
z = np.cos(x)

点和线的样式

颜色

参数color或c

plt.plot(x, y, c='red')
plt.plot(x, z, color='red')
[<matplotlib.lines.Line2D at 0x1008c0f0>]

png

颜色值的方式
  • 别名

    • color=’r’
  • 合法的HTML颜色名

    • color = ‘red’
颜色别名HTML颜色名颜色别名HTML颜色名
蓝色bblue绿色ggreen
红色rred黄色yyellow
青色ccyan黑色kblack
洋红色mmagenta白色wwhite
  • HTML十六进制字符串

    • color = ‘#eeefff’
  • 归一化到[0, 1]的RGB元组

    • color = (0.3, 0.3, 0.4)
plt.plot(x, z, c='m')
[<matplotlib.lines.Line2D at 0x9257860>]

png

# color 可以使用rgba
plt.plot(x,z,c=(0.001, 0.999, 0.555, 0.1))
[<matplotlib.lines.Line2D at 0xa535a90>]

png

透明度

alpha参数

# alpha 代表透明度,取值是浮点型,代表的是百分比
plt.plot(x, z, c=(0.001, 0.999, 0.555), alpha=0.3)
[<matplotlib.lines.Line2D at 0x11100358>]

png

背景色

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

plt.subplot(facecolor='black')
plt.plot(x, z, c=(0.001, 0.999, 0.555))
[<matplotlib.lines.Line2D at 0x11173400>]

png

线型

参数linestyle或ls

线条风格描述线条风格描述
‘-‘实线‘:’虚线
‘–’破折线‘steps’阶梯线
‘-.’点划线‘None’ / ‘,’什么都不画
plt.plot(x, y, c=(0.001, 0.999, 0.555), linestyle='steps')
[<matplotlib.lines.Line2D at 0x11378cf8>]

png

线宽

linewidth或lw参数

plt.plot(x, y, c=(0.001, 0.999, 0.555), linestyle='steps', lw=10)
[<matplotlib.lines.Line2D at 0x113e1b38>]

png

不同宽度的破折线

dashes参数
设置破折号序列各段的宽度
这里写图片描述

x = np.linspace(-np.pi, np.pi, 100)
y = np.sin(x)

# dashes 它的值是一个序列,所填的值的数量必须是偶数
plt.plot(x,y, dashes=[6,6, 2, 2, 4 ,4])
[<matplotlib.lines.Line2D at 0x1152d400>]

png

点型

marker参数

标记描述标记描述
‘1’一角朝下的三脚架‘3’一角朝左的三脚架
‘2’一角朝上的三脚架‘4’一角朝右的三脚架
plt.figure(figsize=(12,4))
x = np.linspace(-np.pi, np.pi, 100)
y = np.sin(x)
plt.plot(x, y, marker='1', markersize=100)
[<matplotlib.lines.Line2D at 0x148a9f60>]

png

标记描述标记描述
’s’正方形‘p’五边形
‘h’六边形1‘H’六边形2
‘8’八边形
plt.plot(x, y, marker='s')
[<matplotlib.lines.Line2D at 0x129d1240>]

png

标记描述标记描述
‘.’‘x’X
‘*’星号‘+’加号
‘,’像素
plt.plot(x, y, marker='+')
[<matplotlib.lines.Line2D at 0x112787b8>]

png

标记描述标记描述
‘o’圆圈‘D’菱形
‘d’小菱形”,’None’,’ ‘,None
plt.plot(x, y, marker='o')
[<matplotlib.lines.Line2D at 0x12a142e8>]

png

标记描述标记描述
‘_’水平线‘|’水平线

这里写图片描述

plt.plot(x, y, marker='|')
[<matplotlib.lines.Line2D at 0x12a9b1d0>]

png

标记 描述 标记 描述
‘v’ 一角朝下的三角形 ‘<’ 一角朝左的三角形
‘^’ 一角朝上的三角形 ‘>’ 一角朝右的三角形

多参数连用

颜色、点型、线型

plt.figure(figsize=(16,4))
plt.subplot(facecolor='pink')
plt.plot(x, y, 'k+:')
[<matplotlib.lines.Line2D at 0x1344b2b0>]

png

更多点和线的设置

参数描述参数描述
color或c线的颜色linestyle或ls线型
linewidth或lw线宽marker点型
markeredgecolor点边缘的颜色markeredgewidth点边缘的宽度
markerfacecolor点内部的颜色markersize点的大小

这里写图片描述

plt.subplot()

x = np.linspace(0, 10, 10)
y = 3 * x - 10
plt.plot(x, y, ls=':', marker='o', markersize=30, c='r', markerfacecolor='purple', markeredgecolor='blue', markeredgewidth=5)
[<matplotlib.lines.Line2D at 0x134b4940>]

png

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

多个曲线同一设置

属性名声明

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

x = np.linspace(-np.pi, np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, x, y2, c='r', ls=':')
[<matplotlib.lines.Line2D at 0x148fcbe0>,
 <matplotlib.lines.Line2D at 0x148fce80>]

png

多个曲线不同设置

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

plt.plot(x,y1,'ro-.', x, y2, 'g+--')
[<matplotlib.lines.Line2D at 0x14974860>,
 <matplotlib.lines.Line2D at 0x14974978>]

png

三种设置方式

向方法传入关键字参数

plt.plot(x,y1,'ro-.', x, y2, 'g+--')

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

axes = plt.subplot()

line1, line2 = plt.plot(x,y1, x, y2)
line1.set_color('r')
line1.set_linestyle('-.')

png

使用setp()方法

line = plt.plot(x, y1)
plt.setp(line,color='purple', linestyle=':')
  • 输出

    [None, None]

png

X、Y轴坐标刻度

xticks()和yticks()方法
这里写图片描述

line = plt.plot(x, y1)
plt.setp(line,color='purple', linestyle=':')

# 设置坐标的刻度
xticks = plt.xticks(np.arange(-4,5), color='green', size=20)
yticks = plt.yticks([-1,0,1],['min','0','max'], color='r',fontsize=20, rotation=60)

png

面向对象方法

set_xticks、set_yticks、set_xticklabels、set_yticklabels方法

axes = plt.subplot()
line = axes.plot(x, y1)

plt.setp(line, color='purple', linestyle=':')

# 修改刻度
axes.set_xticks(np.arange(-4, 5))
# 将x轴的坐标替换abcd这种类型
axes.set_xticklabels('abcdefghi', color='r', size=20, rotation=60)

  • 输出

    [Text(0,0,’a’),
    Text(0,0,’b’),
    Text(0,0,’c’),
    Text(0,0,’d’),
    Text(0,0,’e’),
    Text(0,0,’f’),
    Text(0,0,’g’),
    Text(0,0,’h’),
    Text(0,0,’i’)]

png

正弦余弦

LaTex语法,用 π π 等表达式在图表上写上希腊字母
这里写图片描述

axes = plt.subplot()
line = axes.plot(x, y1, x, y2)
p = np.pi
axes.set_xticks([-p,-p/2, 0, p/2, p])
axes.set_xticklabels(['-$\pi$', '-$\pi$/2', '0', '$\pi$/2', '$\pi$'], size=20, color='r')
  • 输出

    [Text(0,0,’- pi p i ’),
    Text(0,0,’- pi p i /2’),
    Text(0,0,’0’),
    Text(0,0,’ pi p i /2’),
    Text(0,0,’ pi p i ’)]

png

三、2D图形

直方图

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

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

a = np.random.randint(0, 10, size=10)
a
  • 输出

    array([5, 7, 5, 0, 0, 4, 0, 9, 2, 0])

plt.hist(a, bins=100,normed=True)
  • 输出

    (array([4.44444444, 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 1.11111111, 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 1.11111111,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    2.22222222, 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 1.11111111, 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 0. ,
    0. , 0. , 0. , 0. , 1.11111111]),
    array([0. , 0.09, 0.18, 0.27, 0.36, 0.45, 0.54, 0.63, 0.72, 0.81, 0.9 ,
    0.99, 1.08, 1.17, 1.26, 1.35, 1.44, 1.53, 1.62, 1.71, 1.8 , 1.89,
    1.98, 2.07, 2.16, 2.25, 2.34, 2.43, 2.52, 2.61, 2.7 , 2.79, 2.88,
    2.97, 3.06, 3.15, 3.24, 3.33, 3.42, 3.51, 3.6 , 3.69, 3.78, 3.87,
    3.96, 4.05, 4.14, 4.23, 4.32, 4.41, 4.5 , 4.59, 4.68, 4.77, 4.86,
    4.95, 5.04, 5.13, 5.22, 5.31, 5.4 , 5.49, 5.58, 5.67, 5.76, 5.85,
    5.94, 6.03, 6.12, 6.21, 6.3 , 6.39, 6.48, 6.57, 6.66, 6.75, 6.84,
    6.93, 7.02, 7.11, 7.2 , 7.29, 7.38, 7.47, 7.56, 7.65, 7.74, 7.83,
    7.92, 8.01, 8.1 , 8.19, 8.28, 8.37, 8.46, 8.55, 8.64, 8.73, 8.82,
    8.91, 9. ]),
    )

png

条形图

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

bar()、barh()

x = np.arange(10)
y = np.random.randint(0, 20, size=10)
plt.bar(x, y, width=0.5)
<Container object of 10 artists>

png

plt.barh(x, y)
<Container object of 10 artists>

png

饼图

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

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

p = np.array([0.3, 0.35, 0.2])

plt.pie(p)
plt.axis('equal')
  • 输出

    (-1.1173625220934387,
    1.1008267867663541,
    -1.1006187166000545,
    1.1129928311426345)

png

普通各部分占满饼图

普通未占满饼图

饼图阴影、分裂等属性设置

labels参数设置每一块的标签;labeldistance参数设置标签距离圆心的距离(比例值)

autopct参数设置比例值的显示格式(%1.1f%%);pctdistance参数设置比例值文字距离圆心的距离

explode参数设置每一块顶点距圆形的长度(比例值);colors参数设置每一块的颜色;

shadow参数为布尔值,设置是否绘制阴影

labels = np.array(['china', 'usa', 'europe', 'japan', 'canada', 'austrilia', 'russia', 'others'])
gdp = np.array([0.12, 0.18, 0.2, 0.1, 0.05, 0.04, 0.06, 0.3])
pie = plt.pie(gdp, labels=labels, labeldistance=1.1, pctdistance=0.8,
             explode=[0.15, 0, 0, 0, 0, 0, 0, 0], shadow=True, autopct='%.2f%%')

png

散点图

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

scatter()
这里写图片描述

x = np.random.randn(1000)
y = np.random.randn(1000)
size = np.random.randint(10, 100, size=1000)
colors = np.random.random(size=(1000,3))

plt.scatter(x, y, s=size, marker='d',c=colors)
<matplotlib.collections.PathCollection at 0x14af0f98>

png

四、图形内的文字、注释、箭头

控制文字属性的方法:

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对象

图形内的文字

text()
这里写图片描述

注释

annotate()
xy参数设置箭头指示的位置,xytext参数设置注释文字的位置
arrowprops参数以字典的形式设置箭头的样式
width参数设置箭头长方形部分的宽度,headlength参数设置箭头尖端的长度,
headwidth参数设置箭头尖端底部的宽度,
facecolor设置箭头颜色
shrink参数设置箭头顶点、尾部与指示点、注释文字的距离(比例值)
这里写图片描述

箭头

箭头的样式,没必要记

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()

png

五、3D图

曲面图

这里写图片描述

# 导入绘制3D图形轴面 才可以绘制3d图形
from mpl_toolkits.mplot3d.axes3d import Axes3D

生成数据

# z = w1*x + w2*y + b

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

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

# 交叉,水平平面中点
xx,yy = np.meshgrid(x,y)

# 随机构造的,画出来才能知道形状
z = np.sin(xx) + np.cos(yy - 0.5*xx) - np.sin(xx)*np.cos(xx) 
# 绘制图形
plt.figure(figsize=(12,9))
axes3d = plt.subplot(1,2,1,projection = '3d')

axes3d.plot_surface(xx,yy,z)

axes3d.set_xlabel('X',color = 'red',fontsize = 30)
axes3d.set_ylabel('Y')
axes3d.set_zlabel('Z')

axes3d.set_yticks([0,np.pi/2,np.pi,3*np.pi/2,2*np.pi])

axes3d.set_yticklabels(['0','$\pi$/2','$\pi$','3$\pi$/2','2$\pi$'])


axes3d = plt.subplot(1,2,2,projection = '3d')

# 接受的值,就是3d图形
pic_3d = axes3d.plot_surface(xx,yy,z,cmap = 'rainbow')

# 色柱
plt.colorbar(pic_3d,shrink = 0.5)
<matplotlib.colorbar.Colorbar at 0x10a768908>

png

pic_3d
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x114050f60>
axes3d = plt.subplot(projection = '3d')

x = np.random.randint(0,10,size = 10)

y = np.random.randint(10,20,size = 10)

z = np.random.randint(-10,10,size = 10)

axes3d.scatter3D(x,y,z)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x10a276cc0>

png

axes3d = plt.subplot(projection = '3d')
axes3d.bar3d(x,y,z,1,1,1)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x114303080>

png

绘制图形

x = np.arange(10)
y = np.random.randint(0,30,size = 10)
colors = np.random.random(size = (10,3))
plt.bar(x,y,color = colors)
<Container object of 10 artists>

png

玫瑰图/极坐标条形图

这里写图片描述

wind = np.load('Ravenna_wind.npy')
wind
  • 输出

    array([ 10. , 20. , 40. , 90. , 87. , 79. ,
    70. , 80. , 90. , 90. , 105. , 100. ,
    110. , 116.504 , 145.001 , 145.001 , 145.001 , 155.503 ,
    155.503 , 155.503 , 190. , 126.501 , 250. , 99.0009,
    330.003 , 20. , 70. , 70. , 10. , 80. ,
    126. , 100. , 135. , 130. , 160. , 130.501 ,
    190. , 180. , 193.502 , 193.502 , 224.004 , 224.004 ,
    224.004 , 180. , 190. , 254.001 , 257.503 , 190. ,
    159.5 , 100. , 80. , 90. , 80. , 80. ,
    90. , 90. , 90. , 97. , 89. , 88.0147,
    107.004 , 107.004 , 132.503 , 132.503 , 132.503 , 251. ])

#一维的数据可以用直方图画出来
#range 显示0-360之间的
plt.hist(wind,bins = 8,range=[0,360])
  • 输出

    (array([ 5., 12., 24., 9., 11., 4., 0., 1.]),
    array([ 0., 45., 90., 135., 180., 225., 270., 315., 360.]),
    )

png

#提取直方图中的8个值
value,gap = np.histogram(wind,bins = 8,range=[0,360])
value
  • 输出

    array([ 5, 12, 24, 9, 11, 4, 0, 1])

玫瑰图

x
  • 输出

    array([0. , 0.78539816, 1.57079633, 2.35619449, 3.14159265,
    3.92699082, 4.71238898, 5.49778714])

np.pi/4
  • 输出
    0.7853981633974483
np.linspace(0,2*np.pi,8)
  • 输出

    array([0. , 0.8975979 , 1.7951958 , 2.6927937 , 3.5903916 ,
    4.48798951, 5.38558741, 6.28318531])

np.arange(0,2*np.pi,np.pi/4)
  • 输出

    array([0. , 0.78539816, 1.57079633, 2.35619449, 3.14159265,
    3.92699082, 4.71238898, 5.49778714])

# 进行绘制

plt.figure(figsize=(8,8))

plt.axes(polar = True,facecolor = 'green')


colors = np.random.random(size = (8,3))

x = np.arange(0,2*np.pi,np.pi/4)

plt.bar(x,value,width = np.pi/4,color = colors)
<Container object of 8 artists>

png

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值