Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

python - 如何绘制带有分类和数字(基准)轴的折线图?(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.

(在阅读这些原始文档时,我发现没有任何东西与该情节相似。)

The lineplot only takes in numeric values.

(线图仅接受数字值。)

Is currently there an obvious way to make this categorical plot this that I'm missing?

(当前是否有一种明显的方法可以使这种分类图成为我所缺少的?)

在此处输入图片说明

  ask by Roman Lu?trik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...