使用matplotlib为不同的分类级别绘制不同的颜色 [英] plot different color for different categorical levels using matplotlib
问题描述
我有这个数据帧diamonds
,它由像(carat, price, color)
这样的变量组成,并且我想为每个color
绘制一个price
到carat
的散点图,这意味着不同的color
具有图中的颜色不同.
I have this data frame diamonds
which is composed of variables like (carat, price, color)
, and I want to draw a scatter plot of price
to carat
for each color
, which means different color
has different color in the plot.
在R
中使用ggplot
很容易:
ggplot(aes(x=carat, y=price, color=color), #by setting color=color, ggplot automatically draw in different colors
data=diamonds) + geom_point(stat='summary', fun.y=median)
我想知道如何使用matplotlib
在Python中完成此操作吗?
I wonder how could this be done in Python using matplotlib
?
PS:
我了解辅助绘图软件包,例如seaborn
和ggplot for python
,我不喜欢它们,只是想了解是否可以单独使用matplotlib
来完成这项工作; P
I know about auxiliary plotting packages, such as seaborn
and ggplot for python
, and I donot prefer them, just want to find out if it is possible to do the job using matplotlib
alone, ;P
推荐答案
您可以传递plt.scatter
和c
参数,以选择颜色.下面的代码定义了一个colors
字典,用于将您的钻石颜色映射到绘图颜色.
You can pass plt.scatter
a c
argument which will allow you to select the colors. The code below defines a colors
dictionary to map your diamond colors to the plotting colors.
import matplotlib.pyplot as plt
import pandas as pd
carat = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
price = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600]
color =['D', 'D', 'D', 'E', 'E', 'E', 'F', 'F', 'F', 'G', 'G', 'G',]
df = pd.DataFrame(dict(carat=carat, price=price, color=color))
fig, ax = plt.subplots()
colors = {'D':'red', 'E':'blue', 'F':'green', 'G':'black'}
ax.scatter(df['carat'], df['price'], c=df['color'].apply(lambda x: colors[x]))
plt.show()
df['color'].apply(lambda x: colors[x])
有效地将颜色从钻石"映射到绘图".
df['color'].apply(lambda x: colors[x])
effectively maps the colours from "diamond" to "plotting".
(请原谅我没有放置其他示例图像,我认为2足够:P)
您可以使用seaborn
,它是对matplotlib
的包装,默认情况下它看起来更漂亮(基于观点,我知道:P),但还添加了一些绘图功能.
You can use seaborn
which is a wrapper around matplotlib
that makes it look prettier by default (rather opinion-based, I know :P) but also adds some plotting functions.
为此,您可以使用 seaborn.lmplot
使用fit_reg=False
(可以防止它自动进行某种回归).
For this you could use seaborn.lmplot
with fit_reg=False
(which prevents it from automatically doing some regression).
以下代码使用示例数据集.通过选择hue='color'
,您可以告诉seaborn根据您的颜色拆分数据框,然后绘制每种颜色.
The code below uses an example dataset. By selecting hue='color'
you tell seaborn to split your dataframe up based on your colours and then plot each one.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
carat = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
price = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600]
color =['D', 'D', 'D', 'E', 'E', 'E', 'F', 'F', 'F', 'G', 'G', 'G',]
df = pd.DataFrame(dict(carat=carat, price=price, color=color))
sns.lmplot('carat', 'price', data=df, hue='color', fit_reg=False)
plt.show()
如果您不想使用seaborn,则可以使用pandas.groupby
单独获取颜色,然后仅使用matplotlib绘制颜色,但是您必须随手手动分配颜色,我添加了一个下面的示例:
If you don't want to use seaborn then you can use pandas.groupby
to get the colors alone and then plot them using just matplotlib, but you'll have to manually assign colors as you go, I've added an example below:
fig, ax = plt.subplots()
colors = {'D':'red', 'E':'blue', 'F':'green', 'G':'black'}
grouped = df.groupby('color')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='carat', y='price', label=key, color=colors[key])
plt.show()
此代码假定与上述相同的DataFrame,然后根据color
将其分组.然后,对这些组进行迭代,为每个组进行绘制.为了选择一种颜色,我创建了一个colors
词典,该词典可以将菱形颜色(例如D
)映射为真实颜色(例如red
).
This code assumes the same DataFrame as above and then groups it based on color
. It then iterates over these groups, plotting for each one. To select a color I've created a colors
dictionary which can map the diamond color (for instance D
) to a real color (for instance red
).
这篇关于使用matplotlib为不同的分类级别绘制不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!