python刻度增加1个数_Matplotlib添加一个特定的勾号,表示轴的最大多个刻度单次观察...

本文介绍如何在Matplotlib中为图表添加自定义比例标记,即在每个刻度上显示y轴的最大值,无论间隔大小。作者通过实例展示了如何使用`add_scales`函数调整坐标轴并定制颜色和标签。适合数据可视化开发者理解和应用。
摘要由CSDN通过智能技术生成

我尝试将观察结果分别绘制为每个观察值的多个比例,我成功地绘制了以下图表:

23df3b60f2cf670fe93d0919ef9db48d.png

不过,我想添加一个勾号,表示每个刻度中的y-max值,而不管它与上一个刻度之间的间隔。下面是一个这样的图的例子。当它是一个最大的间隔时。在

ada4b097af65194ec4c7445ae2ed8905.png

谢谢,

F

下面是用于生成这些示例的代码。在import numpy as np

import pylab as pl

import matplotlib as plt

import matplotlib.ticker as ticker

import matplotlib.transforms

def add_scales(fig, axes, scales, subplot_reduction_factor=0.1, margin_size=50):

nb_scales = len(scales)

b,l,w,h = zoom_ax.get_position().bounds

_, ymax = axes.get_ylim()

# Saves some space to the right so that we can add our scales

fig.subplots_adjust(right=1-(subplot_reduction_factor)*nb_scales)

for (n, (vmin, vmax, color, label, alignment)) in enumerate(scales):

# Adjust wrt. the orignial figure's scale

nax = fig_zoom.add_axes((b,l,w,(h * alignment) / ymax))

nax.spines['right'].set_position(('outward', -40+n*margin_size))

nax.set_ylim((vmin,vmax))

# Move ticks and label to the right

nax.yaxis.set_label_position('right')

nax.yaxis.set_ticks_position('right')

# Hides everything except yaxis

nax.patch.set_visible(False)

nax.xaxis.set_visible(False)

nax.yaxis.set_visible(True)

nax.spines["top"].set_visible(False)

nax.spines["bottom"].set_visible(False)

# Color stuff

nax.spines['right'].set_color(color)

nax.tick_params(axis='y', colors=color)

nax.yaxis.set_smart_bounds(False)

#nax.yaxis.label.set_color(color)

if label != None:

nax.set_ylabel(None)

if __name__ == '__main__':

a=(np.random.normal(10,5,100))

a=np.linspace(0,100,100)

c=np.linspace(0,80, 100)

d=np.linspace(0,40,100)

fig_zoom = plt.pyplot.figure()

zoom_ax = fig_zoom.add_subplot(1,1,1)

zoom_ax.plot(a,c)

zoom_ax.plot(a,d)

zoom_ax.set_title('Zoom')

zoom_ax.set_xlabel('A')

zoom_ax.set_ylabel('B')

zoom_ax.set_ylim((0,100))

zoom_ax.grid()

add_scales(fig_zoom,

zoom_ax, [(0,.55,'green',None,40),

(0,.85,'blue',None,80)])

fig_zoom.savefig(open('./test.svg','w'),format='svg')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值