Jupyter-Notebook
下载Jupyter,matplotlib
pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib-i https://pypi.tuna.tsinghua.edu.cn/simple
启动jupyter-Notebook服务
jupyter-notebook
会自动弹出
如果没有打开
关闭notebook服务
虚拟机命令行按下快捷键CTRL+C,5秒内输入y关闭服务,否则服务不会关闭需要再按 CTRL+C
注意:如果虚拟机或宿主机电脑开启某些WEB相关服务(如网络代理或去广告服务),可能会导致Jupyter Notebook无法运行
工具栏操作
折线图
先导入包,并点击运行
import matplotlib.pyplot as plt
import random
写入折线代码,并运行
# 指定画版的大小和分辨率
plt.figure(figsize=(6,6), dpi=100)
# 绘制一条线时,x轴可以省略,默认用y轴数据的索引代替
plt.plot([0,2,4,6,8]) # 默认Y轴坐标
plt.plot([0,2,4,6,8],[1,5,3,9,7]) # X轴坐标值,Y轴坐标值
plt.show() #显示图片
运行结果
条形图/柱状图的绘制
代码
# 指定画版的大小和分辨率
plt.figure(figsize=(6,6), dpi=100)
x = [1,2,3,4,5]
y = [3,6,1,8,2]
# 柱状图,x轴为单个柱子,y轴为柱子高度,Width用于柱子粗细
plt.bar(x, y)
plt.show()
# 条形图,注意x,y含义
plt.barh(
x, # 横条离开x轴的距离
y, # 横条长度
height=0.5, # 横条粗细
)
# y轴标注
plt.yticks(x,['a','b','c','d','e'])
# x轴标注
plt.xticks([2,4,6,8,10],['2','4','6','8','10'])
plt.show()
运行效果
直方图
代码
import matplotlib.pyplot as plt
import random
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/home/halon/桌面/caogao/arial unicode ms.ttf")
# 直方图统计电影市场的頻数
plt.figure(figsize=(15, 6), dpi=100)
# 准备时长
time =[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
# 指定组距
width = 5
# 组数
num_bins = int((max(time) - min(time))/width)
# 显示直方图
plt.hist(x=time,bins=num_bins,density=True)
#指定显示刻度的个数
plt.xticks(range(min(time),max(time))[::5])
# 指定标题
plt.title('250个电影的时长分布图',fontdict={"fontproperties":my_font})
plt.grid(True,linestyle="--",alpha=0.5)
plt.show()
效果图
饼图
代码
# 饼图大小
plt.figure(figsize=(15, 6), dpi=100)
# 国名
mark = ['America','China','India','Saudi','Russia','Japan','Britain','Germany','France']
# 各国占9国总军费的比例
percent = [0.5548467,0.14444868,0.05094268,0.04846696,0.046753,0.04418206,0.04161112,0.03799276,0.03075605]
plt.pie(
percent, # 百分比
labels = mark, # 名称
explode=(0,0.1,0,0,0,0,0,0,0), # 突出块,突出比例
autopct='%1.1f%%', # 显示百分比方式
shadow=False, # 阴影效果
startangle=-110, # 饼图起始的角度,度数,默认0为右侧水平180度开始,逆时针旋转
)
plt.axis('equal') #正圆形饼图,x/y轴尺寸相等.默认是扁图,
plt.show()
效果图
离散图
plt.figure(figsize=(10, 6), dpi=100)
my_font = font_manager.FontProperties(fname="/home/halon/桌面/caogao/arial unicode ms.ttf")
# 设置背景色为灰色
plt.rcParams['axes.facecolor'] = '#ebebeb'
# 年龄
age = [34,40,37,30,44,36,32,26,32,36]
# 收入
income = [350,450,169,189,183,80,166,120,75,40]
# 销售额
sales = [123,114,135,139,117,121,133,140,133,133]
# 年龄,销售额,散点图
plt.scatter(age, sales)
# 收入,销售额,散点图
plt.scatter(income, sales)
plt.plot(age[::4],sales[::4])
# 指定标题
plt.title("散点图",fontdict={"fontproperties":my_font})
plt.grid(linewidth=0.2)
plt.show()
效果图