简体   繁体   English

如何绘制带有分类轴和数字(基准)轴的折线图?

[英]How can I plot line graph with categorical and numeric (datum) axes?

Seaborn enables you to create a categorical plot using points Seaborn 使您能够使用点创建分类图

import seaborn as sns

tips = sns.load_dataste('tips')
sns.catplot(x='tip', y='sex', data=tips, jitter=False)

在此处输入图像描述

Is there a way to connect the points with a line for the same gender?有没有办法将相同性别的点与线连接起来?

My goal is to create a plot that will be similar to the below figure (done in R's ggplot2 ).我的目标是创建一个类似于下图的图(在R 的 ggplot2中完成)。 Reading the seaborn documentation I find nothing that would resemble this plot.阅读seaborn 文档后,我发现没有任何内容与此情节相似。 The lineplot only takes in numeric values. 线图只接受数值。 Is currently there an obvious way to make this categorical plot this that I'm missing?目前是否有一种明显的方法可以使这个分类图成为我所缺少的?

在此处输入图像描述

Group by the category and plot each line individually.按类别分组并单独绘制每条线。

import numpy as np
import matplotlib.pyplot as plt

def cat_horizontal_plot(data, category, numeric, ax=None):
    ax = ax or plt.gca()
    for cat, num in data.groupby(category):
        ax.plot(np.sort(num[numeric].values), [cat]*len(num),
                marker="o", mec="k", mfc="none", linestyle="-", color="k")
    ax.set_xlabel(numeric)
    ax.set_ylabel(category)
    ax.margins(y=0.4)
    ax.figure.tight_layout()

Use it as用作

import seaborn as sns
tips = sns.load_dataset('tips')

cat_horizontal_plot(tips, "sex", "tip")
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM