如何在箱线图中分离图 [英] how to unseparate plots in boxplot

查看:46
本文介绍了如何在箱线图中分离图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个包含6个子文件夹的目录绘制了箱形图.当我编写 plt.boxplot(my_list) 并编写 plt.show() 时,它绘制了 6 个不同的图,如果不写,所有 6 个图将相互叠加.如何将它们分开并将它们收集在一张图中?另外,是否可以使用 label=directory 在 X 轴中使用?我写的代码如下:

I have plotted a box plot through a directory that has 6 subfolders within. When I write plt.boxplot(my_list) with writing plt.show() it plots 6 different graphs and without writing that, all 6 plots will overlay on each other. How can I unseparate them and make them collected in one graph? Also, is it possible to use label=directory for using in X-Axis? The code that I have written is below:

import numpy as np
import matplotlib.pyplot as plt
import os
sns.set(style="darkgrid")
root = r'/home/hossein/Desktop/Out/INTERSECTION/BETA 15'
xx=[]
percentage=[]
gg=[]
my_list = os.listdir(root)
my_list =  [file for file in my_list if os.path.isdir(os.path.join(root, file))]
for directory in my_list:
    CASES = [file for file in os.listdir(os.path.join(root, directory)) if file.startswith('config')]   
    if len(CASES)==0:
        continue
    CASES.sort()   
    percentage=[]  
    for filename in CASES:      
        with open(os.path.join(root, directory,filename), "r") as file: 
            lines = file.readlines()
            x = [float(line.split()[0]) for line in lines]
            y = [float(line.split()[1]) for line in lines]
            g = np.linspace(min(y),max(y),100)
            h = min(y)*0.9
            t = max(y)*0.9
            xx=[]
            gg= []
            for i in range(1,len(x)):
                if (y[i] < h or y[i] > t):
                    xx.append(x[i])
            percent = len(xx)/len(y)
            percentage.append(percent)
    plt.boxplot(percentage,)

#     plt.show()    

能否请您提供一些示例

推荐答案

诀窍是将列表传递给 positions.此外,plt.show() 必须在循环外调用.

The trick is passing a list to positions. Also, plt.show() must be called outside the loop.

这是一个简单的例子:

import matplotlib.pyplot as plt

a = [1,2,3,4,5,6]
b = [5,6,7,8,9]

data = [a,b]

for i, x in enumerate(data):
    plt.boxplot(x, positions=[i])
plt.show()

您可以随时更改刻度标签.

You can always change the ticks labels with anything you want.

在这种情况下,你我把[0,1]改为['dir_A','dir_B']:

In this case, you I change [0,1] to ['dir_A','dir_B']:

plt.xticks([0,1], ['dir_A','dir_B'])
plot.show()

这篇关于如何在箱线图中分离图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆