简体   繁体   English

如何在一个 plot 中使用不同范围的 plot 数据框的特定列(也许使用子图?)

[英]how to plot specific columns of data frame with different range in one plot (Maybe using sub-plot?)

I have a data frame that has parameters with different ranges.我有一个具有不同范围参数的数据框。 You can see the sample data below:您可以在下面看到示例数据:

在此处输入图像描述

Since the range of parameters are different if I plot a boxplot for these variables, the result won't look good.由于参数范围不同,如果我 plot 这些变量的箱线图,结果看起来不太好。 I searched and I found this link How to plot boxplots of multiple columns with different ranges which explains plotting using `plotly.我搜索并找到了这个链接How to plot boxplots of multiple columns with different ranges这解释了使用 `plotly. Here is my code following this link这是我在这个链接之后的代码

from plotly.subplots import make_subplots
import plotly.graph_objects as go
vars = ['Current_Market_Cap', 'Last_Price', 'Alpha', "Beta"]
fig = make_subplots(rows=1, cols=len(vars))
for i, var in enumerate(vars):
    fig.add_trace(
        go.Box(y=f_df[var],
        name=var),
        row=1, col=i+1
    )

fig.update_traces(boxpoints='all', jitter=.3)

The result of this code is shown below:这段代码的结果如下所示:

在此处输入图像描述

however, I don't want the interactive mode and what I really need is a simple boxplot for each variable side by side.但是,我不想要交互模式,我真正需要的是并排显示每个变量的简单箱线图。 Basically, I want the same output of plotly but not really fancy and interactive and just with basic python code基本上,我想要与 plotly 相同的 output 但不是很花哨和互动,只是基本的 python 代码

is there anyway doing that?反正有这样做吗? maybe using subplot instead?也许改用subplot

If you need to use matplotlib to create the above multi-range graphs, as you mentioned, you can use subplots like this.如果你需要使用 matplotlib 创建上面的多范围图,正如你提到的,你可以像这样使用子图。 Note that I have used random numbers and so, no outliers.请注意,我使用了随机数,因此没有异常值。 But, the colors should remain the same for the outliers as well.但是,对于异常值,colors 也应该保持不变。

import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(
    {'Current_Market_Cap': np.random.rand(100)*1000000,
     'Last_Price': np.random.rand(100)*100,
     'Alpha': np.random.rand(100),
     'Beta': np.random.rand(100)*10
    })

fig, axs = plt.subplots(1, len(df.columns), figsize=(20,10))
mycolors = ['r', 'b', 'c', 'k']
for i, ax in enumerate(axs.flat):
    ax1 = ax.boxplot(df.iloc[:,i])
    ax.set_title(df.columns[i], fontsize=20, fontweight='bold')
    ax.tick_params(axis='y', labelsize=14)
    for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
        plt.setp(ax1[item], color=mycolors[i])
    
plt.tight_layout()

Output Output

在此处输入图像描述

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

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