“Y轴”标记器之间的分布和间距

2024-04-18 23:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

你好吗?你知道吗

我正在训练做一个在Y轴上有很多标签的图。你知道吗

我有两个问题:

  1. 酒鬼不会沿着图片分布,他们会留下空白
  2. 他们站在一起,没有任何空间

我查看了stackoverflow和matplotlib库的文档,没有找到答案。你知道吗

我试过:

  • 你知道吗plt.tick\参数(长度=15)-长度不同且无差异-
  • 你知道吗plt.tick\参数(衬垫=25)-衬垫不同且无差异-
  • <> LI>使情节的长度变大,这只会使空白空间变大-
  • 你知道吗plt.ylim公司(-1122)-删除空白,但不要在酒杯之间留出空间
  • 还有一些我现在不记得了

代码如下:

def mercVsCapacidad(df, nombre='Test'):

count = len(df.Fecha.unique())


# ============================ Iterando por fechas ===============================
for i in df.Fecha.unique():
    tmp = df[df.Fecha == i]


# ============================== Tamaño dinámico ================================
    if len(tmp) <= 25:
        fig = plt.figure(figsize=(900/96,600/96),dpi=96)
    elif len(tmp) <= 70:
        fig = plt.figure(figsize=(900/96,900/96),dpi=96)
    else:
        fig = plt.figure(figsize=(900/96,1200/96),dpi=96)


# ========================= Agregando celda % de Mercado =========================
    p_mercado = tmp['%\nProducción\nTotal'].sum()
    tmp['% Mercado'] = round(tmp['%\nProducción\nTotal']*100/p_mercado,2)


# ============================== Realizando el plott =============================
    sns.set_style('darkgrid')
    ax = sns.scatterplot(x = '%\n[(Dispon.+ Invers.) /\nDeudas c/Aseg.]',
                         y = 'Denominación',
                         size = '% Mercado',
                         sizes = (100,700),
                         hue = '% Mercado',
                         data = tmp,
                         palette = 'viridis',
                        )


# ================================ Forma del plott ===============================
    plt.suptitle('RELACIÓN % DE MERCADO VS CAPACIDAD DE RESPUESTA', 
                 fontsize=18, fontweight='bold', y = 1)
    plt.ylabel('{}'.format(i), 
               fontsize=16,fontweight='bold')
    plt.xlabel('% de capacidad de respuesta', 
               fontsize=16,fontweight='bold')
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), 
               loc='lower left', ncol=5, 
               mode="expand", borderaxespad=0., )
    plt.tick_params(axis='y', labelsize = 7, labelbottom=True ) # labelrotation=45, length=25, pad = 25,length=15,
    plt.xlim(0,800)
    plt.ylim()


# ============================== Guardando el plott =============================
    nombre_archivo = './plots/MercVsCap_'+nombre+str(count).zfill(4)+'.jpg'       
    plt.savefig(nombre_archivo, dpi=96,bbox_inches='tight')                      
    plt.gca()

    count -= 1
    return

绘图图像: The result of the plot

如果你想查看完整的代码,我给你GitHub: GitHub with full code

谢谢你的阅读,我希望你能帮助我


Tags: dflencountfig空间pltmercadotmp