50题matplotlib从入门到精通-手把手使用Anaconda实践

最近看到文章《50题matplotlib从入门到精通》觉得很有意义,实践如下:
一,编程环境
这里我使用Anaconda3,需要软件的从百度云盘下载。
下载地址:https://pan.baidu.com/s/11WrltUbAV-Z9SvprY91Pxw 提取码: nafe
安装好后,我使用jupyter notebook进行实践。
这里介绍一个较简单的我常用的运行方法:

  • 找到开始菜单中已安装好的Anaconda目录,找到Anaconda Prompt并运行

  • 如我在D盘的pythontest文件中运行和保存文件,只需要输入D:盘符,cd转入pythontest文件夹,运行jupyter notebook,如图(注意运行期间,该窗口不可关闭):
    在这里插入图片描述

  • 即可在网页中打开jupyter notebook。通过右侧的new下拉按钮,选择Python3,即可创建一个python文件"Untitled.ipynb"如图:
    在这里插入图片描述
    我们来实践第一个案例:
    一、导入要使用到的matplotlib库和numpy

导入matplotlib库简写为plt

导入numpy 简写为np,代码如下:

import matplotlib.pyplot as plt
import numpy as np

点击Run,正常运行

二、基本图表

2.用 plot 方法画出 x=(0,10)间 sin 的图像,输入代码:

x = np.linspace(0, 10, 30)
plt.plot(x, np.sin(x));

点击Run,运行效果如图:
在这里插入图片描述

后续案例请按顺序执行,因为部分案例的变量可能依赖前面案例的变量值。

3.用点加线的方式画出x=(0,10)间sin的图像

plt.plot(x, np.sin(x), '-o');

点击Run,运行效果如图:
在这里插入图片描述
页面效果如图:
在这里插入图片描述
4.用scatter方法画出x=(0,10)间sin的点图像

plt.scatter(x, np.sin(x));

在这里插入图片描述
5.用饼图的面积及颜色展示一组4维数据

rng = np.random.RandomState(0)
x = rng.randn(100)
y = rng.randn(100)
colors = rng.rand(100)
sizes = 1000 * rng.rand(100)

plt.scatter(x, y, c=colors, s=sizes, alpha=0.3,
            cmap='viridis')
plt.colorbar(); # 展示色阶

在这里插入图片描述
6.绘制一组误差为±0.8的数据的误差条图

x = np.linspace(0, 10, 50)
dy = 0.8
y = np.sin(x) + dy * np.random.randn(50)
plt.errorbar(x, y, yerr=dy, fmt='.k')

在这里插入图片描述
7.绘制一个柱状图

x = [1,2,3,4,5,6,7,8]
y = [3,1,4,5,8,9,7,2]
label=['A','B','C','D','E','F','G','H']
plt.bar(x,y,tick_label = label);

在这里插入图片描述
8.绘制一个水平方向柱状图

plt.barh(x,y,tick_label = label);//同样使用与7例中的变量,与7对比,只多一个h

在这里插入图片描述
9.绘制1000个随机值的直方图

data = np.random.randn(1000)
plt.hist(data);

在这里插入图片描述
10.设置直方图分30个bins,并设置为频率分布

plt.hist(data, bins=30,histtype='stepfilled', density=True)
plt.show();

在这里插入图片描述
11.在一张图中绘制3组不同的直方图,并设置透明度

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(alpha=0.3, bins=40, density = True)
plt.hist(x1, **kwargs);
plt.hist(x2, **kwargs);
plt.hist(x3, **kwargs);

在这里插入图片描述
12.绘制一张二维直方图

mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
plt.hist2d(x, y, bins=30);

在这里插入图片描述
13.绘制一张设置网格大小为30的六角形直方图

plt.hexbin(x, y, gridsize=30);

在这里插入图片描述

三、自定义图表元素

14.绘制x=(0,10)间sin的图像,设置线性为虚线

x = np.linspace(0,10,100)
plt.plot(x,np.sin(x),'--');

在这里插入图片描述
15设置y轴显示范围为(-1.5,1.5)

x = np.linspace(0,10,100)
plt.plot(x, np.sin(x))
plt.ylim(-1.5, 1.5);

在这里插入图片描述
16.设置x,y轴标签variable x,value y

x = np.linspace(0.05, 10, 100)
y = np.sin(x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值