python绘图matplotlib——使用记录1

本博文来自于网络收集,如有侵权请联系删除


matplotlib的具体用法 官网的介绍是最全的,此处只针对平时常用的绘图进行了记录

1 常用函数汇总

1.1 plot

reference:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
函数:plt.plot(x, y, c=‘r’, marker=‘o’, ls=‘-’, lw=1, ms=1, label=‘line1’)
参数:

  • x: x轴上的数值
  • y: y轴上的数值
  • marker:点的形状
  • c: color, 颜色
  • ls: Line Styles,如,‘-’,‘–’,‘-.’,‘:’
  • lw: linewidth,线条宽度
  • label: 标记图形内容的标签文本

注,plt.plot(x, y, ‘r^:’, lw=1, ms=5, label=‘line1’)
点的颜色、形状、线型通常写在一起,如
label需要与plt.legend配合使用,

1.2 legend

reference:https://matplotlib.org/stable/api/legend_api.html
函数:plt.legend(loc=‘best’)
参数:

  • loc:图例在途中的位置,如,‘upper right’, ‘lower left’, '‘upper center’'等

代码示例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, marker='o', ls='-', c='r', lw=1, ms=1, label='line1')
# plt.plot(x, y, 'r^:', lw=1, ms=5, label='line1')
plt.legend(loc='best')
plt.show()

1.3 scatter

函数:plt.scatter(x, y, c=‘b’, label=‘scatter1’)
参数:

  • x: x轴上的数值
  • y: y轴上的数值
  • c:颜色
  • label: 标记图形内容的标签文本
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.05, 10, 50)
y = np.sin(x)
y2 = np.cos(x)

plt.scatter(x, y, c='b', label='scatter1')
plt.scatter(x, y2, c='r', label='scatter1')
plt.legend()
plt.show()

在这里插入图片描述

1.4 xlim

函数:plt.xlim(xmin, xmax)
参数:

  • xmin: x轴上的最小值
  • ymin: x轴上的最大值
  • 平移性:plt.ylim(ymin, ymax)

1.5 xlabel

函数:plt.xlabel(string)
参数:

  • string: 标签文本内容
  • 平移性:plt.ylabel(string)

1.6 grid

函数:plt.grid(linestyle=‘:’, color="r)
参数:

  • linestyle: 网格线的线条风格
  • color: 网格线的线条颜色

1.7 axhline

绘制平行与x轴的水平参考线
函数:plt.axhline(y=0, c=‘b’, ls=‘–’, lw=2)
参数:

  • y: 水平参考线的出发点
  • c: 参考线的线条颜色
  • ls: 参考线的线条风格
  • lw: 参考线的线条宽度
  • 平移性: plt.axvline(), 绘制平行与y轴的参考线

1.7 axvspan

绘制垂直于x轴的参考区域。
函数:plt.axvspan(xmin=1, xmax=2, facecolor=‘y’, alpha=0.3)
参数:

  • xmin: 参考区域的起始位置。
  • ymin: 参考区域的终止位置。
  • facecolor: 参看区域的填充颜色。
  • alpha: 参考区域的填充颜色的透明度。
  • 平移性: axhspan()
import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls='-.', c="c", label="sin(x)")

plt.legend()

plt.axvspan(xmin=4.0, xmax=6.0, facecolor="y", alpha=0.3)
plt.axhspan(ymin=0.0, ymax=0.5, facecolor="y", alpha=0.3)

plt.show()

在这里插入图片描述

1.8 annotate

添加图形内容细节的指向型注释文本。
函数:plt.annotate(“maximum”, xy=(np.pi / 2, 1), xytext=((np.pi / 2) + 1, .8), weight=“bold”, color=‘b’, arrowprops=dict(arrowstyle=“->”, connectionstyle=“arc3”, color=“b”))
参数:

  • string: 图形内容的注释文本。
  • xy: 被注释图形内容的位置坐标。
  • xytext: 注释文本的位置坐标。
  • weight: 注释文本的字体粗细风格。
  • color: 注释文本的字体颜色。
  • arrowprops: 指示被注释内容的箭头的属性字典。
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="g", label='sin(x)')
plt.legend()

plt.annotate("maximum", xy=(np.pi / 2, 1), xytext=((np.pi / 2) + 1, .8), weight="bold", color='black',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="r"))

plt.show()

在这里插入图片描述

1.9 text

添加图形内容细节的无指向型注释文本
函数:plt.text(4, 0.1, “y=sin(x)”, weight=“bold”, color=“b”)
参数:

  • x: 注释文本内容所在位置的横坐标
  • y: 注释文本内容所在位置的纵坐标
  • string: 注释文本内容
  • weight: 注释文本内容的粗细风格
  • color: 注释文本内容的字体颜色
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls='-.', lw=2, c="c", label="sin(x)")

plt.legend()
plt.text(4, 0.1, "y=sin(x)", weight="bold", color="b")
plt.show()

在这里插入图片描述

1.10 title

添加图形内容的标题
函数:plt.title(string)
参数:

  • string: 图形内容的标题文本

2 常见图形绘制

2.1 bar——柱状图

函数:plt.bar(x, y, align=“center”, color=“c”, tick_label=x_label, hatch=“/”)
参数:

  • x: 柱状图中的柱体标签值。
  • y: 柱状图中的柱体高度。
  • align: 柱体对齐方式
  • color: 柱体颜色
  • tick_label: 刻度标签值
  • alpha: 柱体的透明度
  • hatch: 柱体的填充样式,如"/", “\”, “|”, "-"等,符号字符串的符号数量越多,柱体的几何图形的密集程度就越高。
import matplotlib as mpl
import matplotlib.pyplot as plt
from plotly.figure_factory import np

mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

x = [i for i in range(0, 10, 1)]
y = np.random.randint(1, 20, size=10)

x_label = [chr(i) for i in range(97, 97+10, 1)]
plt.bar(x, y, align="center", color="c", tick_label=x_label, hatch="/")

plt.xlabel(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值