python多个箱线图

阅读: 评论:0

python多个箱线图

python多个箱线图

loc确定缩放轴的位置,1表示upper right,2表示upper left等等。我稍微修改了示例代码以生成多个缩放轴。import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes

from mpl_toolkits.axes_grid1.inset_locator import mark_inset

import numpy as np

def get_demo_image():

from matplotlib.cbook import get_sample_data

import numpy as np

f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)

z = np.load(f)

# z is a numpy array of 15x15

return z, (-3,4,-4,3)

fig = plt.figure(1, [5,4])

ax = fig.add_subplot(111)

# prepare the demo image

Z, extent = get_demo_image()

Z2 = np.zeros([150, 150], dtype="d")

ny, nx = Z.shape

Z2[30:30+ny, 30:30+nx] = Z

# extent = [-3, 4, -4, 3]

ax.imshow(Z2, extent=extent, interpolation="nearest",

origin="lower")

axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6

axins.imshow(Z2, extent=extent, interpolation="nearest",

origin="lower")

# sub region of the original image

x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9

axins.set_xlim(x1, x2)

axins.set_ylim(y1, y2)

axins1 = zoomed_inset_axes(ax, 8, loc=2) # zoom = 6

axins1.imshow(Z2, extent=extent, interpolation="nearest",

origin="lower")

# sub region of the original image

x1, x2, y1, y2 = -1.2, -0.9, -2.2, -1.9

axins1.set_xlim(x1, x2)

axins1.set_ylim(y1, y2)

# draw a bbox of the region of the inset axes in the parent axes and

# connecting lines between the bbox and the inset axes area

mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")

mark_inset(ax, axins1, loc1=2, loc2=4, fc="none", ec="0.5")

plt.draw()

plt.show()

编辑1:

同样,也可以在boxplot中添加缩放轴。下面是一个例子from pylab import *

from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes

from mpl_toolkits.axes_grid1.inset_locator import mark_inset

# fake up some data

spread= rand(50) * 100

center = ones(25) * 50

flier_high = rand(10) * 100 + 100

flier_low = rand(10) * -100

data =concatenate((spread, center, flier_high, flier_low), 0)

# fake up some more data

spread= rand(50) * 100

center = ones(25) * 40

flier_high = rand(10) * 100 + 100

flier_low = rand(10) * -100

d2 = concatenate( (spread, center, flier_high, flier_low), 0 )

data.shape = (-1, 1)

d2.shape = (-1, 1)

data = [data, d2, d2[::2,0]]

# multiple box plots on one figure

fig = plt.figure(1, [5,4])

ax = fig.add_subplot(111)

ax.boxplot(data)

ax.set_xlim(0.5,5)

ax.set_ylim(0,300)

# Create the zoomed axes

axins = zoomed_inset_axes(ax, 3, loc=1) # zoom = 3, location = 1 (upper right)

axins.boxplot(data)

# sub region of the original image

x1, x2, y1, y2 = 0.9, 1.1, 125, 175

axins.set_xlim(x1, x2)

axins.set_ylim(y1, y2)

# draw a bbox of the region of the inset axes in the parent axes and

# connecting lines between the bbox and the inset axes area

mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")

show()

编辑2

如果分布是不均匀的,即大多数值很小,很少有非常大的值,则上述缩放过程可能不起作用,因为它将同时缩放x轴和y轴。在这种情况下,最好将y-axis的刻度改为log。from pylab import *

# fake up some data

spread= rand(50) * 1

center = ones(25) * .5

flier_high = rand(10) * 100 + 100

flier_low = rand(10) * -100

data =concatenate((spread, center, flier_high, flier_low), 0)

# fake up some more data

spread= rand(50) * 1

center = ones(25) * .4

flier_high = rand(10) * 100 + 100

flier_low = rand(10) * -100

d2 = concatenate( (spread, center, flier_high, flier_low), 0 )

data.shape = (-1, 1)

d2.shape = (-1, 1)

data = [data, d2, d2[::2,0]]

# multiple box plots on one figure

fig = plt.figure(1, [5,4]) # Figure Size

ax = fig.add_subplot(111) # Only 1 subplot

ax.boxplot(data)

ax.set_xlim(0.5,5)

ax.set_ylim(.1,300)

ax.set_yscale('log')

show()

本文发布于:2024-02-01 08:29:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674735635249.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:线图   多个   python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23