Python boxplot matplotlib基于类别数的自动图形大小 [英] Python boxplot matplotlib automatic figure size based on the number of categories

查看:67
本文介绍了Python boxplot matplotlib基于类别数的自动图形大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 matplotlib 来显示和保存箱线图.

I need to use matplotlib to show and save a boxplot chart.

但是类别的数量是可变的,所以我不能有一个固定的 figsize 并且必须根据类别的数量调整图形大小(画布区域).

But the number of categories is variable, so that, I cannot have a fixed figsize and the figure size (canvas area) must be adjusted based on number of categories.

我正在努力使其以动态方式工作.

I'm struggling to get this working in a dynamic fashion.

当我只有几个类别时,图表还可以,但是下面的情况(131个类别)中,我得到以下图表:

When I have just a few categories the chart is ok, but the case below (131 categories) I'm getting the following chart:

所以,我想我应该以某种方式设置单个框的大小(或空系列的占位符),然后图表根据 box_size * number_of_classes 增长.

So, I imagine that I should, somehow, set the size of a single box (or a placeholder for an empty serie) and then the chart grows based on the box_size * number_of_classes.

下面是我试过的一些代码.

Below some code I tried.

f = plt.figure()
# f = plt.figure(figsize=(len(classes) * 2, 50))

# Create an axes instance
ax = f.add_subplot(111)

## add patch_artist=True option to ax.boxplot() 
## to get fill color
bp = ax.boxplot(data_to_plot, patch_artist=True)

## change outline color, fill color and linewidth of the boxes
for box in bp['boxes']:
    # change outline color
    box.set( color='#7570b3', linewidth=2)
    # change fill color
    box.set( facecolor = '#1b9e77' )

## change color and linewidth of the whiskers
for whisker in bp['whiskers']:
    whisker.set(color='#7570b3', linewidth=2)

## change color and linewidth of the caps
for cap in bp['caps']:
    cap.set(color='#7570b3', linewidth=2)

## change color and linewidth of the medians
for median in bp['medians']:
    median.set(color='#b2df8a', linewidth=2)

## change the style of fliers and their fill
for flier in bp['fliers']:
    flier.set(marker='o', color='#e7298a', alpha=0.5)

## Custom x-axis labels
ax.set_xticklabels(classes)

## Remove top axes and right axes ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()        

if boxplot_output is not None:
    os.makedirs(boxplot_output, exist_ok=True)
    f.savefig(os.path.join(boxplot_output,'box_plot.png'), bbox_inches='tight')

if show:
    plt.show()
    plt.close(f)

我如何使它工作?

最好的问候.克莱森里奥斯.

Best Regards. Kleyson Rios.

推荐答案

以英寸为单位定义左右边距.选择您希望一个类别显示的英寸数.那么整个图形的宽度是

Define the left and right margin in inches. Choose how large in inches you want one single category to display. Then the complete figure width is

figwidth = leftmargin + rightmargin + (n+1)*categorysize

然后不要忘记根据图形大小调整子图参数.

Then don't forget to adjust the subplot parameters depending on the figure size.

import numpy as np
import matplotlib.pyplot as plt

number = 10
data = np.random.rayleigh(scale=30, size=(20, number))


leftmargin = 0.5 #inches
rightmargin = 0.3 #inches
categorysize = 0.1 # inches

n = data.shape[1]

figwidth = leftmargin + rightmargin + (n+1)*categorysize

fig, ax = plt.subplots(figsize=(figwidth, 4))
fig.subplots_adjust(left=leftmargin/figwidth, right=1-rightmargin/figwidth,
                    top=0.94, bottom=0.1)

ax.boxplot(data, positions=np.arange(n))
ax.set_xlim(-0.5,n-0.5)

plt.show()

对于数字= 10:

对于 number=42

For number=42

这篇关于Python boxplot matplotlib基于类别数的自动图形大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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