Search code examples
pythonboxplotsubplot

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:

enter image description here

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:

enter image description here

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?


Solution

  • 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

    enter image description here