如何为每个目标创建单独的箱形图?

时间:2020-09-16 20:01:11

标签: python plotly

我正在尝试为数据集中的某些变量和每个子图创建一组箱线图,我希望为每个目标值创建一个单独的箱线图。因此,对于每个子图中的每个目标值,我将具有三个箱形图。有谁知道如何做到这一点?下面的代码是我目前拥有的。

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
from sklearn import datasets
from typing import Tuple, List
import plotly.graph_objects as go
from plotly.subplots import make_subplots

def load_data() -> Tuple[np.ndarray, np.ndarray, List[str]]:
"""Load the wine dataset

Returns:
    features: the dataset features
    target: the labels of the dataset
    feature_names: names of each feature
"""
wine = datasets.load_wine()
features = wine['data']
target = wine['target']
feature_names = wine['feature_names']
return features, target, feature_names

features, target, feature_names = load_data()

Data = {
    feature_names[0]:features[:,0].tolist(),
    feature_names[1]:features[:,1].tolist(),
    feature_names[2]:features[:,2].tolist(),
    feature_names[3]:features[:,3].tolist(),
    feature_names[4]:features[:,4].tolist(),
    'Target': target.tolist()
}
Data = pd.DataFrame(data = Data)

fig = make_subplots(rows = 5,cols = 1)
for i in range(5):
    fig.append_trace(go.Box(
        x = Data[feature_names[i]],
        name = feature_names[i],
        marker_color='DarkSlateGrey',
        showlegend = False
    ),col = 1,row = i+1)

fig.update_traces(orientation = "h")
fig.show()

enter image description here

0 个答案:

没有答案