使用matplotlib中scatter方法畫散點(diǎn)圖
本文實(shí)例為大家分享了用matplotlib中scatter方法畫散點(diǎn)圖的具體代碼,供大家參考,具體內(nèi)容如下
1、最簡(jiǎn)單的繪制方式
繪制散點(diǎn)圖是數(shù)據(jù)分析過程中的常見需求。python中最有名的畫圖工具是matplotlib,matplotlib中的scatter方法可以方便實(shí)現(xiàn)畫散點(diǎn)圖的需求。下面我們來繪制一個(gè)最簡(jiǎn)單的散點(diǎn)圖。
數(shù)據(jù)格式如下:
0 746403
1 1263043
2 982360
3 1202602
...
其中第一列為X坐標(biāo),第二列為Y坐標(biāo)。下面我們來畫圖。
#!/usr/bin/env python
#coding:utf-8
import matplotlib.pyplot as plt
def pltpicture():
file = "xxx"
xlist = []
ylist = []
with open(file, "r") as f:
for line in f.readlines():
lines = line.strip().split()
if len(lines) != 2 or int(lines[1]) < 100000:
continue
x, y = int(lines[0]), int(lines[1])
xlist.append(x)
ylist.append(y)
plt.xlabel('X')
plt.ylabel('Y')
plt.scatter(xlist, ylist)
plt.show()

2、更漂亮一些的畫圖方式
上面的圖片比較粗糙,是最簡(jiǎn)單的方式,沒有任何相關(guān)的配置項(xiàng)。下面我們?cè)儆昧硗庖环輸?shù)據(jù)集畫出更漂亮一點(diǎn)的圖。
數(shù)據(jù)集來自網(wǎng)絡(luò)的公開數(shù)據(jù)集,數(shù)據(jù)格式如下:
40920 8.326976 0.953952 3
14488 7.153469 1.673904 2
26052 1.441871 0.805124 1
75136 13.147394 0.428964 1
...
第一列每年獲得的飛行??屠锍虜?shù);
第二列玩視頻游戲所耗時(shí)間百分比;
第三列每周消費(fèi)的冰淇淋公升數(shù);
第四列為label:
1表示不喜歡的人
2表示魅力一般的人
3表示極具魅力的人
現(xiàn)在將每年獲取的飛行里程數(shù)作為X坐標(biāo),玩視頻游戲所消耗的事件百分比作為Y坐標(biāo),畫出圖。
from matplotlib import pyplot as plt
file = "/home/mi/wanglei/data/datingTestSet2.txt"
label1X, label1Y, label2X, label2Y, label3X, label3Y = [], [], [], [], [], []
with open(file, "r") as f:
for line in f:
lines = line.strip().split()
if len(lines) != 4:
continue
distance, rate, label = lines[0], lines[1], lines[3]
if label == "1":
label1X.append(distance)
label1Y.append(rate)
elif label == "2":
label2X.append(distance)
label2Y.append(rate)
elif label == "3":
label3X.append(distance)
label3Y.append(rate)
plt.figure(figsize=(8, 5), dpi=80)
axes = plt.subplot(111)
label1 = axes.scatter(label1X, label1Y, s=20, c="red")
label2 = axes.scatter(label2X, label2Y, s=40, c="green")
label3 = axes.scatter(label3X, label3Y, s=50, c="blue")
plt.xlabel("every year fly distance")
plt.ylabel("play video game rate")
axes.legend((label1, label2, label3), ("don't like", "attraction common", "attraction perfect"), loc=2)
plt.show()
最后效果圖:

3、scatter函數(shù)詳解
我們來看看scatter函數(shù)的簽名:
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None,
verts=None, edgecolors=None,
**kwargs):
"""
Make a scatter plot of `x` vs `y`
Marker size is scaled by `s` and marker color is mapped to `c`
Parameters
----------
x, y : array_like, shape (n, )
Input data
s : scalar or array_like, shape (n, ), optional
size in points^2. Default is `rcParams['lines.markersize'] ** 2`.
c : color, sequence, or sequence of color, optional, default: 'b'
`c` can be a single color format string, or a sequence of color
specifications of length `N`, or a sequence of `N` numbers to be
mapped to colors using the `cmap` and `norm` specified via kwargs
(see below). Note that `c` should not be a single numeric RGB or
RGBA sequence because that is indistinguishable from an array of
values to be colormapped. `c` can be a 2-D array in which the
rows are RGB or RGBA, however, including the case of a single
row to specify the same color for all points.
marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'
See `~matplotlib.markers` for more information on the different
styles of markers scatter supports. `marker` can be either
an instance of the class or the text shorthand for a particular
marker.
cmap : `~matplotlib.colors.Colormap`, optional, default: None
A `~matplotlib.colors.Colormap` instance or registered name.
`cmap` is only used if `c` is an array of floats. If None,
defaults to rc `image.cmap`.
norm : `~matplotlib.colors.Normalize`, optional, default: None
A `~matplotlib.colors.Normalize` instance is used to scale
luminance data to 0, 1. `norm` is only used if `c` is an array of
floats. If `None`, use the default :func:`normalize`.
vmin, vmax : scalar, optional, default: None
`vmin` and `vmax` are used in conjunction with `norm` to normalize
luminance data. If either are `None`, the min and max of the
color array is used. Note if you pass a `norm` instance, your
settings for `vmin` and `vmax` will be ignored.
alpha : scalar, optional, default: None
The alpha blending value, between 0 (transparent) and 1 (opaque)
linewidths : scalar or array_like, optional, default: None
If None, defaults to (lines.linewidth,).
verts : sequence of (x, y), optional
If `marker` is None, these vertices will be used to
construct the marker. The center of the marker is located
at (0,0) in normalized units. The overall marker is rescaled
by ``s``.
edgecolors : color or sequence of color, optional, default: None
If None, defaults to 'face'
If 'face', the edge color will always be the same as
the face color.
If it is 'none', the patch boundary will not
be drawn.
For non-filled markers, the `edgecolors` kwarg
is ignored and forced to 'face' internally.
Returns
-------
paths : `~matplotlib.collections.PathCollection`
Other parameters
----------------
kwargs : `~matplotlib.collections.Collection` properties
See Also
--------
plot : to plot scatter plots when markers are identical in size and
color
Notes
-----
* The `plot` function will be faster for scatterplots where markers
don't vary in size or color.
* Any or all of `x`, `y`, `s`, and `c` may be masked arrays, in which
case all masks will be combined and only unmasked points will be
plotted.
Fundamentally, scatter works with 1-D arrays; `x`, `y`, `s`, and `c`
may be input as 2-D arrays, but within scatter they will be
flattened. The exception is `c`, which will be flattened only if its
size matches the size of `x` and `y`.
Examples
--------
.. plot:: mpl_examples/shapes_and_collections/scatter_demo.py
"""
其中具體的參數(shù)含義如下:
x,y是相同長(zhǎng)度的數(shù)組。
s可以是標(biāo)量,或者與x,y長(zhǎng)度相同的數(shù)組,表明散點(diǎn)的大小。默認(rèn)為20。
c即color,表示點(diǎn)的顏色。
marker 是散點(diǎn)的形狀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python學(xué)習(xí)之matplotlib繪制散點(diǎn)圖實(shí)例
- Python+matplotlib繪制不同大小和顏色散點(diǎn)圖實(shí)例
- Python?Matplotlib實(shí)現(xiàn)三維數(shù)據(jù)的散點(diǎn)圖繪制
- Python利用matplotlib繪制散點(diǎn)圖的新手教程
- Python使用Matplotlib繪制三維散點(diǎn)圖詳解流程
- python3使用matplotlib繪制散點(diǎn)圖
- Python matplotlib繪制散點(diǎn)圖的實(shí)例代碼
- 使用matplotlib畫散點(diǎn)圖的方法
- matplotlib一維散點(diǎn)分布圖的實(shí)現(xiàn)
相關(guān)文章
python 內(nèi)置庫(kù)wsgiref的使用(WSGI基礎(chǔ)入門)
WSGI(web服務(wù)器網(wǎng)關(guān)接口)主要規(guī)定了服務(wù)器端和應(yīng)用程序之間的接口,即規(guī)定了請(qǐng)求的URL到后臺(tái)處理函數(shù)之間的映射該如何實(shí)現(xiàn)。wsgiref是一個(gè)幫助開發(fā)者開發(fā)測(cè)試的Python內(nèi)置庫(kù),程序員可以通過這個(gè)庫(kù)了解WSGI的基本運(yùn)行原理,但是不能把它用在生產(chǎn)環(huán)境上。2021-06-06
python爬取網(wǎng)站數(shù)據(jù)保存使用的方法
這篇文章主要介紹了使用Python從網(wǎng)上爬取特定屬性數(shù)據(jù)保存的方法,其中解決了編碼問題和如何使用正則匹配數(shù)據(jù)的方法,詳情看下文2013-11-11
python實(shí)現(xiàn)windows壁紙定期更換功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)windows壁紙定期更換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
html網(wǎng)頁(yè)調(diào)用后端python代碼的方法實(shí)例
html頁(yè)面中確實(shí)能夠調(diào)用python程序,不過只能調(diào)“一點(diǎn)點(diǎn)”,下面這篇文章主要給大家介紹了關(guān)于html網(wǎng)頁(yè)調(diào)用后端python代碼的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程
這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python設(shè)計(jì)模式結(jié)構(gòu)型享元模式
這篇文章主要介紹了Python享元模式,享元模式即Flyweight Pattern,指運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對(duì)象,下面和小編一起進(jìn)入文章了解更多詳細(xì)內(nèi)容吧2022-02-02

