Matplotlib基本使用

本文介绍了Matplotlib的基本使用,包括安装、引用、设置绘图样式、颜色、标签、标记点大小、线条粗细以及轴限制。还展示了散点图的绘制和风格设置。
摘要由CSDN通过智能技术生成

说明

本文运行环境

  • macOS 10.13.6
  • python 3.7
  • Matplotlib 3.0.2

Matplotlib

安装

conda install -c conda-forge matplotlib 

引用

import numpy as np
import matplotlib.pyplot as plt

设置notebook

%matplotlib inline 

绘制正弦函数

x = np.linspace(0,10,200)
y = np.sin(x)

plt.plot(x,y)

output:
在这里插入图片描述

在正弦函数上再绘制余弦函数

plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))

output:
在这里插入图片描述

设置绘图的样式为虚线

plt.plot(x,y,'--')

output:
在这里插入图片描述
设置绘图的样式为圆点

x = np.linspace(0,10,100)
y = np.sin(x)

plt.plot(x,y,'o')

output:
在这里插入图片描述

设置绘图的样式为六边形

x = np.linspace(0,10,100)
y = np.sin(x)

plt.plot(x,y,'p')

output:
在这里插入图片描述

将绘制图片存储

figure = plt.figure()
plt.plot(x,y,'--')
figure.savefig("/Users/No_notion/Desktop/sinx.png")

#output
一张图片存储到Destop上

同时打印多张图片


#subplot同时打印多张图片,传入三个参数
#第一个参数,代表几行
#第二个参数,代表几列
#第三个参数,代表位置的索引
plt.subplot(3,3,1)
plt.plot(x,np.sin(x),'--')

plt.subplot(3,3,2)
plt.plot(x,np.cos(x),'--')

plt.subplot(3,3,3)
plt.plot(x,np.cos(x),'--')

plt.subplot(3,3,4)
plt.plot(x,np.cos(x),'--')

plt.subplot(3,3,5)
plt.plot(x,np.cos(x),'--')

plt.subplot(3,3,6)
plt.plot(x,np.cos(x),'--')

output:
在这里插入图片描述

设置绘制的颜色为红色

x = np.linspace(0,10,50)
y = np.sin(x)

plt.plot(x,y,'o',color='red')

output:
在这里插入图片描述

颜色值支持十六进制字符串

设置标签

x = np.linspace(0,10,100)

plt.plot(x,np.sin(x),label='sinx(x)')

plt.plot(x,np.cos(x),'--',label='cos(x)')
plt.legend()

output:
| 在这里插入图片描述

将标签设置在右上角

x = np.linspace(0,10,100)

plt.plot(x,np.sin(x),label='sinx(x)')

plt.plot(x,np.cos(x),'--',label='cos(x)')
plt.legend(loc='upper right')

output:
在这里插入图片描述

标签位置列表

位置字符串位置码描述
‘best’0自动
‘upper right’1右上
‘upper left’2左上
‘lower left’3左下
‘lower right’4右下
‘right’5
‘center left’6中左
‘center right’7中右
‘lower center’8中下
‘upper center’9中上
‘center’10中心

设置标记点大小

plt.subplot(2,2,1)
plt.plot(x,np.sin(x),'-p',color='red',markersize=3)
         
plt.subplot(2,2,2)
plt.plot(x,np.cos(x),'-p',color='green',markersize=5)
         
plt.subplot(2,2,3)
plt.plot(x,np.cos(x),'-p',color='orange',markersize=8)
         
plt.subplot(2,2,4)
plt.plot(x,np.cos(x),'-p',color='pink',markersize=10)

output:
在这里插入图片描述

设置标记点边线颜色

plt.plot(x,np.sin(x),'-p',color='red',markersize=3,markeredgecolor='black')

output:
在这里插入图片描述

设置绘制线条的粗细

plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)

output:
在这里插入图片描述

设置y轴的限制条件

plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)

#第一个参数为最小值
#第二个参数为最大值
plt.ylim(-0.5,0.5)

output:
在这里插入图片描述

设置x轴的限制条件

plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)
plt.ylim(-1.0,0.5)
plt.xlim(2,8)

output:
在这里插入图片描述

散点图

#设置x
x = np.random.randn(100)
#设置y
y = np.random.randn(100)
#设置color
colors = np.random.randn(100)
#设置sizes
sizes = np.random.rand(100)*1000
plt.scatter(x=x,y=y,s=sizes,c=colors)

output:
在这里插入图片描述

散点图其他属性

x = np.random.randn(100)
y = np.random.randn(100)
colors = np.random.randn(100)
sizes = np.random.rand(100)*1000
#设置alpha
alpha = 0.4
plt.scatter(x=x,y=y,s=sizes,c=colors,alpha = alpha)
#设置colorBar
plt.colorbar()

output:
在这里插入图片描述
设置绘制风格

plt.style.use('classic')
x = np.random.randn(100)
y = np.random.randn(100)
colors = np.random.randn(100)
sizes = np.random.rand(100)*1000
#设置alpha
alpha = 0.4
plt.scatter(x=x,y=y,s=sizes,c=colors,alpha = alpha)
#设置colorBar
plt.colorbar()

output:
在这里插入图片描述

相关链接

matplotlib Documentation
matplotlib tutorial from tutorialspoint

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值