简体   繁体   中英

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. I searched and I found this link How to plot boxplots of multiple columns with different ranges which explains plotting using `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

is there anyway doing that? maybe using subplot instead?

If you need to use matplotlib to create the above multi-range graphs, as you mentioned, you can use subplots like this. Note that I have used random numbers and so, no outliers. But, the colors should remain the same for the outliers as well.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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