第十三章 Matplotlib库

第十三章 Matplotlib库

数据可视化是数据分析的一个重要工具,掌声有请Matplotlib

13.0 环境配置

【1】 要不要plt.show()

  • ipython中可用魔术方法 %matplotlib inline

  • pycharm 中必须使用plt.show()

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid")  # 设置下面都是用这种风格
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.ylabel("squares")
# plt.show()   
Text(0, 0.5, 'squares')

在这里插入图片描述

【2】设置样式

plt.style.available[:5]  # 查看可以使用的风格
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight']
with plt.style.context("seaborn-white"):  # with代码块中使用这个风格
    plt.plot(x, y)

在这里插入图片描述

【3】将图像保存为文件

import numpy as np
x = np.linspace(0, 10 ,100)
plt.plot(x, np.exp(x))
plt.savefig("my_figure.png")

在这里插入图片描述

13.1 Matplotlib库

13.1.1 折线图

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid")
import numpy as np
x = np.linspace(0, 2*np.pi, 100)  # 0-2Π中间100个数
plt.plot(x, np.sin(x))
[<matplotlib.lines.Line2D at 0x18846169780>]

在这里插入图片描述

  • 绘制多条曲线
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.cos(x))
plt.plot(x, np.sin(x))
[<matplotlib.lines.Line2D at 0x1884615f9e8>]

在这里插入图片描述

【1】调整线条颜色和风格

  • 调整线条颜色
offsets = np.linspace(0, np.pi, 5)
colors = ["blue", "g", "r", "yellow", "pink"]
for offset, color in zip(offsets, colors):
    plt.plot(x, np.sin(x-offset), color=color)         # color可缩写为c

在这里插入图片描述

  • 调整线条风格
x = np.linspace(0, 10, 11)
offsets = list(range(8))
linestyles = ["solid", "dashed", "dashdot", "dotted", "-", "--", "-.", ":"]
for offset, linestyle in zip(offsets, linestyles):
    plt.plot(x, x+offset, linestyle=linestyle)        # linestyle可简写为ls

在这里插入图片描述

  • 调整线宽
x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
linewidths = (i*2 for i in range(1,5))
for offset, linewidth in zip(offsets, linewidths):
    plt.plot(x, x+offset, linewidth=linewidth)                 # linewidth可简写为lw

在这里插入图片描述

  • 调整数据点标记
x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
markers = ["*", "+", "o", "s"]
for offset, marker in zip(offsets, markers):
    plt.plot(x, x+offset, marker=marker)   

在这里插入图片描述

x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
markers = ["*", "+", "o", "s"]
for offset, marker in zip(offsets, markers):
    plt.plot(x, x+offset, marker=marker, markersize=10)      # markersize可简写为ms

在这里插入图片描述

  • 颜色跟风格设置的简写
x = np.linspace(0, 10, 11)
offsets = list(range(0, 8, 2))
color_linestyles = ["g-", "b--", "k-.", "r:"]
for offset, color_linestyle in zip(offsets, color_linestyles):
    plt.plot(x, x+offset, color_linestyle)

在这里插入图片描述

x = np.linspace(0, 10, 11)
offsets = list(range(0, 8, 2))
color_marker_linestyles = ["g*-", "b+--", "ko-.", "rs:"]
for offset, color_marker_linestyle in zip(offsets, color_marker_linestyles):
    plt.plot(x, x+offset, color_marker_linestyle)

在这里插入图片描述

其他用法及颜色缩写、数据点标记缩写等请查看官方文档,如下:

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

【2】调整坐标轴

  • xlim, ylim
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.xlim(-1, 7)  # 坐标轴最大最小值范围
plt.ylim(-1.5, 1.5)
(-1.5, 1.5)

在这里插入图片描述

  • axis
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis([-2, 8, -2, 2])  # 同时设置x轴和y轴,前两个是x轴,后两个是y轴
[-2, 8, -2, 2]

在这里插入图片描述

x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("tight")  # 还可以设置图像的风格---紧致
(0.0, 6.283185307179586, -0.9998741276738751, 0.9998741276738751)

在这里插入图片描述

x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("equal")  # 扁平
(0.0, 7.0, -1.0, 1.0)

在这里插入图片描述

?plt.axis
  • 对数坐标
x = np.logspace(0, 5, 100)
plt.plot(x, np.log(x))
plt.xscale("log")

在这里插入图片描述

  • 调整坐标轴刻度
x = np.linspace(0, 10, 100)
plt.plot(x, x**2)
plt.xticks(np.arange(0, 12, step=1))  # 直接设置坐标轴的刻度

在这里插入图片描述

x = np.linspace(0, 10, 100)
plt.plot(x, x**2)
plt.xticks(np.arange(0, 12, step=1), fontsize=15)
plt.yticks(np.arange(0, 110, step=10))

在这里插入图片描述

  • 调整刻度样式
x = np.linspace(0, 10, 100)
plt.plot(x, x**2
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值