首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python:在一个图形中绘制多个散点图

Python:在一个图形中绘制多个散点图
EN

Stack Overflow用户
提问于 2018-05-26 16:38:47
回答 1查看 5.4K关注 0票数 2

我有一个具有多个类别的数据集,我想要在单个图形中绘制,以查看某些内容是如何变化的。我在数据集中有一个给定类别的列表,我希望在同一张图中看到所有这些类别

代码语言:javascript
复制
sample = [
['For business', 0.7616104043587437],
['For home and cottages', 0.6890139579274699],
['Consumer electronics', 0.039868871866136635],
['Personal things', 0.7487893699793786],
['Services', 0.747226678171249],
['Services', 0.23463661173977313],
['Animals', 0.6504301798258314],
['For home and cottages', 0.49567857024037665],
['For home and cottages', 0.9852681814098107],
['Transportation', 0.8134867587477912],
['Animals', 0.49988690699674654],
['Consumer electronics', 0.15086800344617235],
['For business', 0.9485494576819328],
['Hobbies and Leisure', 0.25766871111905243],
['For home and cottages', 0.31704508627659533],
['Animals', 0.6192114570078333],
['Personal things', 0.5755788287287359],
['Hobbies and Leisure', 0.10106922056341394],
['Animals', 0.16834618003738577],
['Consumer electronics', 0.7570803588496894]
]
train = pd.DataFrame(data=sample,  columns=['parent_category_name','deal_probability'])
parent_categories = train['parent_category_name'].unique()
parent_categories_size = len(parent_categories)
fig, ax = plt.subplots(figsize=(12,10))
colors = iter(cm.rainbow(np.linspace(0, 1, parent_categories_size)))

for parent_category_n in range(parent_categories_size):
    parent_1 = train[train['parent_category_name'] == parent_categories[parent_category_name]]
    ax.scatter(
        range(parent_1.shape[0]), 
        np.sort(parent_1.deal_probability.values),
        color = next(colors)
    )
plt.ylabel('likelihood that an ad actually sold something', fontsize=12)
plt.title('Distribution of likelihood that an ad actually sold something')

我不知道为什么我只能看到最后一个图,而不是所有的图。或者,我可以在一个图形中使用多个散点图,但我很难尝试将其绘制出来。

目前我正在处理10个类别,但我正在努力使其具有动态性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 05:48:02

如果您想要观察随着时间的推移的发展,使用带有标记的线形图可能更好地可视化每个类别中的变化:

代码语言:javascript
复制
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.cm as cm

sample = [  ['For business', 0.7616104043587437],
            ['For home and cottages', 0.6890139579274699],
            ['Consumer electronics', 0.039868871866136635],
            ['Personal things', 0.7487893699793786],
            ['Services', 0.747226678171249],
            ['Services', 0.23463661173977313],
            ['Animals', 0.6504301798258314],
            ['For home and cottages', 0.49567857024037665],
            ['For home and cottages', 0.9852681814098107],
            ['Transportation', 0.8134867587477912],
            ['Animals', 0.49988690699674654],
            ['Consumer electronics', 0.15086800344617235],
            ['For business', 0.9485494576819328],
            ['Hobbies and Leisure', 0.25766871111905243],
            ['For home and cottages', 0.31704508627659533],
            ['Animals', 0.6192114570078333],
            ['Personal things', 0.5755788287287359],
            ['Hobbies and Leisure', 0.10106922056341394],
            ['Animals', 0.16834618003738577],
            ['Consumer electronics', 0.7570803588496894] ]

train = pd.DataFrame(data=sample,  columns=['parent_category_name','deal_probability'])
parent_categories = train['parent_category_name'].unique()

fig, ax = plt.subplots(figsize=(10,8))
colors = iter(cm.rainbow(np.linspace(0, 1, len(parent_categories))))

for parent_category in parent_categories:
    ax.plot(range(len(train[train["parent_category_name"] == parent_category])), 
            sorted(train[train["parent_category_name"] == parent_category].deal_probability.values),
            color = next(colors),
            marker = "o",
            label = parent_category)

plt.ylabel('likelihood that an ad actually sold something', fontsize=12)
plt.title('Distribution of likelihood that an ad actually sold something')
plt.legend(loc = "best")
plt.show()

输出:

但由于这是一个任意的尺度,而且你对数据进行了排序,在我看来,你甚至可以更好地在分类图中看到分布:

代码语言:javascript
复制
train = pd.DataFrame(data=sample,  columns=['parent_category_name','deal_probability'])
parent_categories = train['parent_category_name'].unique()

fig, ax = plt.subplots(figsize=(18,9))
colors = iter(cm.rainbow(np.linspace(0, 1, len(parent_categories))))

for parent_category in parent_categories:
    ax.scatter(
        train[train["parent_category_name"] == parent_category].parent_category_name.values, 
        train[train["parent_category_name"] == parent_category].deal_probability.values,
        color = next(colors),
        label = parent_category
    )

plt.ylabel('likelihood that an ad actually sold something', fontsize=12)
plt.title('Distribution of likelihood that an ad actually sold something')
plt.legend(loc = "best")
plt.show()

输出:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50540904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档