Scategory_scatter: 创建一个散点图,不同颜色表示不同类别
一个快速生成由类别着色的散点图的函数,可以从 pandas DataFrame
或 NumPy ndarray
对象中获取数据。
> 来自 mlxtend.general 的 category_scatter
概述
参考文献
- -
示例 1 - 来自 Pandas DataFrames 的类别散点图
import pandas as pd
from io import StringIO
csvfile = """标签,x,y
类别1,10.0,8.04
类别1,10.5,7.30
类别2,8.3,5.5
类别2,8.1,5.9
类别3,3.5,3.5
类别3,3.8,5.1"""
df = pd.read_csv(StringIO(csvfile))
df
label | x | y | |
---|---|---|---|
0 | class1 | 10.0 | 8.04 |
1 | class1 | 10.5 | 7.30 |
2 | class2 | 8.3 | 5.50 |
3 | class2 | 8.1 | 5.90 |
4 | class3 | 3.5 | 3.50 |
5 | class3 | 3.8 | 5.10 |
绘制数据,其中类别由 label_col
列中的唯一值决定。x
和 y
值只是我们想要绘制的 DataFrame 的列名。
import matplotlib.pyplot as plt
from mlxtend.plotting import category_scatter
fig = category_scatter(x='x', y='y', label_col='label',
data=df, legend_loc='upper left')
示例 2 - 来自 NumPy 数组的类别散点图
import numpy as np
from io import BytesIO
csvfile = """1,10.0,8.04
1,10.5,7.30
2,8.3,5.5
2,8.1,5.9
3,3.5,3.5
3,3.8,5.1"""
ary = np.genfromtxt(BytesIO(csvfile.encode()), delimiter=',')
ary
array([[ 1. , 10. , 8.04],
[ 1. , 10.5 , 7.3 ],
[ 2. , 8.3 , 5.5 ],
[ 2. , 8.1 , 5.9 ],
[ 3. , 3.5 , 3.5 ],
[ 3. , 3.8 , 5.1 ]])
现在,假设第一列代表标签,第二列和第三列分别代表 x
和 y
值。
import matplotlib.pyplot as plt
from mlxtend.plotting import category_scatter
fix = category_scatter(x=1, y=2, label_col=0,
data=ary, legend_loc='upper left')
API
category_scatter(x, y, label_col, data, markers='sxo^v', colors=('blue', 'green', 'red', 'purple', 'gray', 'cyan'), alpha=0.7, markersize=20.0, legend_loc='best')
Scatter plot to plot categories in different colors/markerstyles.
Parameters
-
x
: str or intDataFrame column name of the x-axis values or integer for the numpy ndarray column index.
-
y
: strDataFrame column name of the y-axis values or integer for the numpy ndarray column index
-
data
: Pandas DataFrame object or NumPy ndarray. -
markers
: strMarkers that are cycled through the label category.
-
colors
: tupleColors that are cycled through the label category.
-
alpha
: float (default: 0.7)Parameter to control the transparency.
-
markersize
: float (default` : 20.0)Parameter to control the marker size.
-
legend_loc
: str (default: 'best')Location of the plot legend {best, upper left, upper right, lower left, lower right} No legend if legend_loc=False
Returns
fig
: matplotlig.pyplot figure object
Examples
For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/plotting/category_scatter/