python脊椎图举例说明_制作标签、bbox或轴.text在python matplotlib中与图的脊椎齐平...

本文通过示例展示了如何在Python的Matplotlib库中创建脊椎图,并精确地将标签放置在右上角或左上角,使标签的边框与图形脊椎对齐。代码详细调整了脊椎的宽度、标签的位置和样式,以实现对不同大小和布局图表的适应性。
摘要由CSDN通过智能技术生成

对于某个手稿,我需要将图表的标签精确地放在右上角或左上角。标签需要一个与图形的脊椎具有相同厚度的边框。目前我是这样做的:import matplotlib.pyplot as plt

import numpy as np

my_dpi=96

xposr_box=0.975

ypos_box=0.94

nrows=3

Mytext="label"

GLOBAL_LINEWIDTH=2

fig, axes = plt.subplots(nrows=nrows, sharex=True, sharey=True, figsize=

(380/my_dpi, 400/my_dpi), dpi=my_dpi)

fig.subplots_adjust(hspace=0.0001)

colors = ('k', 'r', 'b')

for ax, color in zip(axes, colors):

data = np.random.random(1) * np.random.random(10)

ax.plot(data, marker='o', linestyle='none', color=color)

for ax in ['top','bottom','left','right']:

for idata in range(0,nrows):

axes[idata].spines[ax].set_linewidth(GLOBAL_LINEWIDTH)

axes[0].text(xposr_box, ypos_box , Mytext, color='black',fontsize=8,

horizontalalignment='right',verticalalignment='top', transform=axes[0].transAxes,

bbox=dict(facecolor='white', edgecolor='black',linewidth=GLOBAL_LINEWIDTH))

plt.savefig("Label_test.png",format='png', dpi=600,transparent=True)

f3df0c3931262ad50c9c38d641fcaa67.png

因此,我用以下参数控制长方体的位置:

^{pr2}$

如果更改绘图的宽度,则长方体的位置也会更改,但其顶部和右侧(或左侧)脊椎应始终位于图形脊椎的正上方:import matplotlib.pyplot as plt

import numpy as np

my_dpi=96

xposr_box=0.975

ypos_box=0.94

nrows=3

Mytext="label"

GLOBAL_LINEWIDTH=2

fig, axes = plt.subplots(nrows=nrows, sharex=True, sharey=True, figsize=

(500/my_dpi, 400/my_dpi), dpi=my_dpi)

fig.subplots_adjust(hspace=0.0001)

colors = ('k', 'r', 'b')

for ax, color in zip(axes, colors):

data = np.random.random(1) * np.random.random(10)

ax.plot(data, marker='o', linestyle='none', color=color)

for ax in ['top','bottom','left','right']:

for idata in range(0,nrows):

axes[idata].spines[ax].set_linewidth(GLOBAL_LINEWIDTH)

axes[0].text(xposr_box, ypos_box , Mytext, color='black',fontsize=8,

horizontalalignment='right',verticalalignment='top',transform=axes[0].transAxes,

bbox=dict(facecolor='white', edgecolor='black',linewidth=GLOBAL_LINEWIDTH))

plt.savefig("Label_test.png",format='png', dpi=600,transparent=True)

cdbd6ebba7e2a7fde7abd6529a3785dd.png

如果图像是窄的而不是像这个例子中的更宽,也应该是这种情况。我希望避免手动操作。有没有办法总是把它放在应该放的地方?独立于地块的宽度和高度

堆积图的数量呢?在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值