Python数据可视化,Matplotlib绘制“散点图”的两种方法!

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

前言

散点图是Matplotlib常用图形之一,与线形图类似。但是这种图形不再由线段连接,而是由独立的点、圆圈或其他形状构成。那么怎么画散点图呢?Matplotlib给出了两种不同的方法,去画散点图。如何在不同的情况下,合理的使用这两种方法?

进群:700341555获取Python入门学习资料!

用plt.plot画散点图

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import numpy as np
import matplotlib.pyplot as plt
x= np.linspace(0, 10, 30)
y= np.sin(x)
plt.plot(x, y, ‘o’, color=’black’);
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

plt.plot()函数的第三个参数是一个字符,表示图形符号的类型。与你之前用 ‘-‘ 和 ‘–‘ 设置线条属性类似,对应的图形标记也有缩写形式。

1.部分图形标记展示

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>rng= np.random.RandomState(0)
formarkerin[‘o’, ‘.’, ‘,’, ‘x’, ‘+’, ‘v’, ‘^’, ‘<‘, ‘>’, ‘s’, ‘d’]:
plt.plot(rng.rand(5), rng.rand(5), marker,
label=”marker='{0}'”.format(marker))
plt.legend(numpoints=1)
plt.xlim(0, 1.8);
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

2.连接每一个点

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>plt.plot(x, y, ‘-ok’)# 直线(-)、圆圈(o)、黑色(k)
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

用图形标记的缩写形式,跟线段组合成一给新的字符,传给plt.plot()函数

3.自定义线条和散点属性

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>plt.plot(x, y, ‘-p’, color=’gray’,
markersize=15, linewidth=4,
markerfacecolor=’white’,
markeredgecolor=’gray’,
markeredgewidth=2)
plt.ylim(-1.2, 1.2)
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

plt.plot函数非常灵活,可以满足各种不同的可视化配置需求。

用plt.scatter画散点图

这是另一个创建散点图的函数是plt.scatter。它的功能非常强大,其用法与plt.plot函数类似。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>plt.scatter(x, y, marker=’o’)
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

plot与scatter的区别

plt.scatter与plt.plot的主要差别在于,前者在创建散点图时具有更高的灵活性,可以 单独控制每个散点与数据匹配,也可以让每个散点具有不同的属性(大小、表面颜色、边 框颜色等)。

1.随机散点图

创建一个随机散点图,里面有各种颜色和大小的散点。为了能更好地显示重叠部分,用alpha参数来调整透明度。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>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(); # 显示颜色条
</pre>

《Python数据可视化,Matplotlib绘制“散点图”的两种方法!》 Python数据可视化,Matplotlib绘制“散点图”的两种方法!

注意点,颜色自动映射成颜色条(color scale,通过colorbar()显示),散点的大小以像素为单位。这样,散点的颜色与大小就可以在可视化图中显示多维数据的信息了。

plot与scatter:效率对比

plt.plot 与 plt.scatter 除了特征上的差异之外,还有什么影响我们选择的因素呢?在数 据量较小的时候,两者在效率上的差异不大。但是当数据变大到几千个散点时,plt.plot 的效率将大大高于 plt.scatter。这是由于 plt.scatter 会对每个散点进行单独的大小与颜 色的渲染,因此渲染器会消耗更多的资源。而在 plt.plot 中,散点基本都彼此复制,因此整个数据集中所有点的颜色、尺寸只需要配置一次。由于这两种方法在处理大型数据集时有很大的性能差异,因此面对大型数据集时,plt.plot 方法比 plt.scatter 方法好。

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/c61317018fea
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞