python 图片自动分类_Python boxplot matplotlib根据类别数量自动调整图形大小

在Python中使用matplotlib绘制箱线图时,遇到问题:当类别数量变化时,需要动态调整图形大小。当前代码适用于少量类别,但面对131个类别时,图形显示不理想。尝试通过设置boxplot的参数和调整figsize来解决问题,但未成功。寻求如何根据类别数量自动调整箱线图的大小和布局。
摘要由CSDN通过智能技术生成

我需要使用matplotlib来显示和保存一个箱线图图表。在

但是类别的数量是可变的,因此,我不能有固定的figsize,图形大小(画布区域)必须根据类别的数量进行调整。在

我正在努力使这个工作在一个动态的时尚。在

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

f44704e41efef83b6ecc8a1544fb4214.png

所以,我想我应该,以某种方式,设置一个框的大小(或者为空系列设置一个占位符),然后图表会根据长方体大小*类的数量而增长。在

下面是我试过的代码。在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)

我怎么能让它工作?在

谨致问候。

克莱森·里奥斯。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值