简体   繁体   English

分类和数值数据的混合 pyplot

[英]Mixture of categorical and numerical data pyplot

I am going through the Titanic example in Kaggle and I just wanted to ask about a Python thing - I've looked around for this, but I don't seem to find anything.我正在浏览 Kaggle 中的泰坦尼克号示例,我只是想询问有关 Python 的事情 - 我已经四处寻找这个,但我似乎没有找到任何东西。

Say I have a DataFrame with columns that include numerical and categorical data.假设我有一个 DataFrame,其列包含数字和分类数据。 I am trying to create 3 subplots, each of displaying this data.我正在尝试创建 3 个子图,每个子图都显示这些数据。 Something like this:像这样的东西:

fig, ax = plt.subplots(3,1, figsize=(20,20))
ax[0].hist(train_data['Age'])
ax[1].scatter(train_data.PassengerId,train_data.Cabin)
ax[2].scatter...

I don't have a problem of creating these plots separately, pyplot displays the strings in Cabin vs PassengerId numbers, but it just can't deal with plotting them on one figure.我没有单独创建这些图的问题,pyplot 在 Cabin vs PassengerId 数字中显示字符串,但它无法处理将它们绘制在一个数字上。 Even looping and creating new figures for each plot doesn't seem to work.即使为每个 plot 循环和创建新图形似乎也不起作用。

Am I doing something wrong, or is this how pyplot works?我做错了什么,或者这就是 pyplot 的工作原理? Does it "set" the type of the data at the first line of the code?它是否在代码的第一行“设置”了数据的类型? Can I use some kind of.astype() method to workaround this?我可以使用某种 .astype() 方法来解决这个问题吗?

I'm not exactly sure what you are trying to do, but plotting different things in different panels of a figure (or different things in the same panel) is something that can be easily done with matplotlib.我不完全确定您要做什么,但是使用 matplotlib 可以轻松地在图形的不同面板中绘制不同的东西(或在同一面板中绘制不同的东西)。 See an example below using seborn's copy of titanic data (it also shows how to use seaborn to create statistical plots easily):请参阅下面使用 seborn 的泰坦尼克号数据副本的示例(它还显示了如何使用 seaborn 轻松创建统计图):

import matplotlib.pyplot as plt
import seaborn as sns

titanic = sns.load_dataset('titanic')

fig, ax = plt.subplots(3, 1, figsize=(8, 10))
ax[0].hist(titanic['age'])
ax[1].scatter(titanic.age, titanic.fare)
sns.pointplot(x='class', y='survived', data=titanic, hue='sex', axis=ax[2])

This is the image you should see (provided you have seaborn installed):这是您应该看到的图像(前提是您安装了 seaborn): 在此处输入图像描述

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

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