簡體   English   中英

使用 pandas 在 2 個變量之間繪制圖表

[英]Plotting a graph between 2 variables using pandas

我需要 plot 我的數據集的各種屬性之間的圖表,根據我目前的知識,我所知道的只是直方圖,因為我只能采用一個變量。

我試過這個簡單的代碼:

sn.catplot(x="CDR", y="Age", hue="M/F", data=df);
plt.title('Distribution of Age by CDR rate')

這是我得到的錯誤

CDR 是臨床痴呆分級。
我確實有 R 語言的代碼,他們首先將它們全部分組,然后繪制圖表,但我發現更復雜,所以我決定用這種方式 go。

這是我需要的圖表類型。

我對這個錯誤一無所知。 我嘗試了 df.CDR、df.age、df.M/F,但由於名稱“M/F”而出現錯誤。

'DataFrame' object 沒有屬性 'M'

我嘗試在數據集中更改名稱,但這會產生更多錯誤。

如果需要 R 代碼

幫助!!!! df.head()

在 Python 中,使用 seaborn.violinplot

https://seaborn.pydata.org/generated/seaborn.violinplot.html

下面的完整示例(來自 seaborn 文檔):

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

sns.set_theme(style="whitegrid")

tips = sns.load_dataset("tips")

df = pd.DataFrame([[0.0, 0.0, 0.0, 0.2, 0.2, 0.2, 0.2], [87, 88, 89, 60, 55, 58, 59],
                   ["M", "M", "F", "F", "F", "F", "M"]]).T
df.columns = ["CDR", "Age", "M/F"]

df = df.astype({"CDR": "float64", "Age": "int64"}) # Update type

sns.violinplot(x="CDR", y="Age", hue="M/F", data=df)
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM