美文网首页
数据分析之 matplotlib 绘图

数据分析之 matplotlib 绘图

作者: 王镇_ee87 | 来源:发表于2021-01-11 09:39 被阅读0次
plt.plot()
  • 绘制单线条图形
  • 绘制多线条图形
  • 设置坐标系的比例 plt.figure(figsize=(a,b))
  • 设置图例 legend()
  • 图例保存
    1. fig = plt.figure()
    2. plt.plot(x,y)
    3. figure.savefig()
x = np.linspace(0,10,num=5)
y = (x+2)/2

plt.plot(x,y)
plt.plot(x+1,y-1)
# plt.plot(x,y,x+1,y-1)  等价于上面
plt.show()
image.png

# 设置坐标系比例
plt.figure(figsize=(3,4))  # x, y 按等比例拉伸

plt.plot(x,y)
plt.plot(x+1,y-1)
image.png
# 设置图例名称 legend()

plt.plot(x, y, label="xy")
plt.plot(x + 1, y - 1, label="ab")
plt.legend()
plt.show()
image.png
# 加入标识
plt.xlabel("x")
plt.ylabel("y")
plt.title("temp")

image.png

# 1 生成对象
fig = plt.figure()

# 2 画图像

# 设置坐标系比例
# plt.figure(figsize=(3, 4))

# 添加标识
plt.xlabel("x")
plt.ylabel("y")
plt.title("temp")
# 设置图例名称 legend()
plt.plot(x, y, label="xy")
plt.plot(x + 1, y - 1, label="ab")
# plt.plot(x,y,x+1,y-1)  等价于上面
plt.legend()
plt.show()

# 3 保存图像

fig.savefig('./plt_demo.png', dpi=100) # dpi 像素点 越大越清晰
image.png

# 设置线段样式
plt.plot(x, y, c="green", alpha=0.2, ls="dashed") # alpha 清晰度
plt.show()

image.png
plot 参数 参考自
  • 也可以对关键字参数color赋十六进制的RGB字符串如 color='#900302'
    =============    ===============================
    character        color
    =============    ===============================
    ``'b'``          blue 蓝
    ``'g'``          green 绿
    ``'r'``          red 红
    ``'c'``          cyan 蓝绿
    ``'m'``          magenta 洋红
    ``'y'``          yellow 黄
    ``'k'``          black 黑
    ``'w'``          white 白
    =============    ===============================
  • 点型参数Markers,如:marker='+' 这个只有简写,英文描述不被识别
=============    ===============================
    character        description
    =============    ===============================
    ``'.'``          point marker
    ``','``          pixel marker
    ``'o'``          circle marker
    ``'v'``          triangle_down marker
    ``'^'``          triangle_up marker
    ``'<'``          triangle_left marker
    ``'>'``          triangle_right marker
    ``'1'``          tri_down marker
    ``'2'``          tri_up marker
    ``'3'``          tri_left marker
    ``'4'``          tri_right marker
    ``'s'``          square marker
    ``'p'``          pentagon marker
    ``'*'``          star marker
    ``'h'``          hexagon1 marker
    ``'H'``          hexagon2 marker
    ``'+'``          plus marker
    ``'x'``          x marker
    ``'D'``          diamond marker
    ``'d'``          thin_diamond marker
    ``'|'``          vline marker
    ``'_'``          hline marker
    =============    ===============================

  • 线型参数Line Styles,linestyle='-'
    =============    ===============================
    character        description
    =============    ===============================
    ``'-'``          solid line style 实线
    ``'--'``         dashed line style 虚线
    ``'-.'``         dash-dot line style 点画线
    ``':'``          dotted line style 点线
    =============    ===============================

柱状图
  • 参数: 第一个参数是索引。第二个参数是数据值。第三个参数是条形的宽度

# 柱状图

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

# plt.bar(x,y)
plt.barh(x,y)

plt.show()

plt.bar(x,y)


image.png

plt.barh(x,y)


image.png
直方图
  • 是一个特殊的柱状图,又叫周密图
  • plt.hist()的参数
    1. bins:可以是一个bin数量的整数值,也可以是表示bin的一个序列。默认值是10
    2. normed: 如果值为 True,直方图的值将进行归一化处理,形成概率密度,默认值为False
    3. color: 指定直方图的颜色。可以是单一颜色或颜色的序列。如果制定了多个数据集合,例如DataFrame对象,颜色序列将会设置为相同的顺序。如果未指定,将会使用一个默认的线条颜色
    4. orientation: 通过设置orientation为 horizontal创建水平直方图。默认值为 vertical
# 直方图
x = [1, 2, 3, 2, 4, 5, 1, 1, 1, 2, 4]

plt.hist(x)
plt.show()
image.png
饼图
  • pie() ,饼图也只有一个参数 x
  • 饼图适合展示各部分占总体的比例,条形图适合比较各部分的大小
arr = [11, 22, 31, 15]
plt.pie(arr)

plt.show()
image.png
不完整饼图 加各部分名称
plt.pie(arr , labels=['a','b','c']) # 不完整饼图 加各部分名称
plt.pie(arr)

plt.show()

image.png
labeldistance 名称离圆心距离 越小越近 autopct 每部分占比例 保留几位小数
arr = [0.1,0.5,0.2]
plt.pie(arr , labels=['a','b','c'] , labeldistance=0.1, autopct='%.2f%%')  # labeldistance 名称离圆心距离 越小越近  autopct 每部分占比例 保留几位小数
 
plt.show()

image.png
散点图 scatter()
  • 因变量随自变量而变化的大致趋势
# 散点图

x = np.linspace(-np.pi, np.pi, 10)  # xy 关系 抛物线 检测x y 是否有关系
y = x**2
plt.scatter(x,y)
plt.show()

相关文章

网友评论

      本文标题:数据分析之 matplotlib 绘图

      本文链接:https://www.haomeiwen.com/subject/gbobwktx.html