多个y轴转换比例

Dav*_*ave 4 python plot matplotlib data-conversion axis-labels

具有

具有共享y轴的转换比例

你好

我正在尝试创建在y轴上包含两组单位的并行转换比例的图表; 使用两种不同的风格:

  1. 偏移('寄生')y轴和
  2. 重叠/共享的y轴

复制附加示例图像中左手y轴的样式.

我想找到生成上述两个示例图的最简单的通用方法,这也允许我通过将两组单位之间的关系定义为函数来生成y轴转换比例(在此示例中:mmHg = kPa*7.5).

如果可以添加这些示例中显示的与左手刻度无关的第三个右手y轴(蒸汽浓度和含水量),这将是一个奖励.

我已经阅读了相关的stackoverflow.com帖子和使用twinx和twiny函数的多个x和y轴的例子 - 例如 这里 - 以及Matplotlib食谱,但我找不到解决这个特殊问题的例子.

我会非常感谢任何最小的工作示例或链接.

我在Spyder 2.2.1/Python 2.7.5中使用Matplotlib

非常感谢您的期待

戴夫

Sch*_*sch 7

对于第一个情节,我建议axisartist.y左侧的两轴自动缩放是通过适用于指定y限制的简单缩放因子实现的.第一个例子是基于寄生虫轴的解释:

import numpy as np
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

# initialize the three axis:
host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(left=0.25)

par1 = host.twinx()
par2 = host.twinx()

# secify the offset for the left-most axis:
offset = -60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="left", axes=par2, offset=(offset, 0))
par2.axis["right"].toggle(all=True)

# data ratio for the two left y-axis:
y3_to_y1 = 1/7.5

# y-axis limits:
YLIM = [0.0, 150.0,
        0.0, 150.0]

# set up dummy data
x = np.linspace(0,70.0,70.0)
y1 = np.asarray([xi**2.0*0.032653 for xi in x])
y2 = np.asarray([xi**2.0*0.02857 for xi in x])

# plot data on y1 and y2, respectively:
host.plot(x,y1,'b')
par1.plot(x,y2,'r')

# specify the axis limits:
host.set_xlim(0.0,70.0)
host.set_ylim(YLIM[0],YLIM[1])
par1.set_ylim(YLIM[2],YLIM[3])

# when specifying the limits for the left-most y-axis
# you utilize the conversion factor:
par2.set_ylim(YLIM[2]*y3_to_y1,YLIM[3]*y3_to_y1)

# set y-ticks, use np.arange for defined deltas
# add a small increment to the last ylim value
# to ensure that the last value will be a tick
host.set_yticks(np.arange(YLIM[0],YLIM[1]+0.001,10.0))
par1.set_yticks(np.arange(YLIM[2],YLIM[3]+0.001,10.0))
par2.set_yticks(np.arange(YLIM[2]*y3_to_y1,YLIM[3]*y3_to_y1+0.001, 2.0))

plt.show()
Run Code Online (Sandbox Code Playgroud)

你最终会得到这个情节:

第一个数字

您可以尝试修改上面的示例,以便为您提供第二个图.一个想法是,减少offset到零.但是,使用时axisartist,不支持某些刻度函数.其中之一是指定刻度线是在轴的内部还是外部.
因此,对于第二个图,以下示例(基于matplotlib:具有不同比例的叠加图?)是合适的.

import numpy as np
import matplotlib.pyplot as plt

# initialize the three axis:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax3 = ax1.twinx()

# data ratio for the two left y-axis:
y3_to_y1 = 1/7.5

# y-axis limits:
YLIM = [0.0, 150.0,
        0.0, 150.0]

# set up dummy data
x = np.linspace(0,70.0,70.0)
y1 = np.asarray([xi**2.0*0.032653 for xi in x])
y2 = np.asarray([xi**2.0*0.02857 for xi in x])

# plot the data
ax1.plot(x,y1,'b')
ax2.plot(x,y2,'r')

# define the axis limits
ax1.set_xlim(0.0,70.0)
ax1.set_ylim(YLIM[0],YLIM[1])
ax2.set_ylim(YLIM[2],YLIM[3])

# when specifying the limits for the left-most y-axis
# you utilize the conversion factor:
ax3.set_ylim(YLIM[2]*y3_to_y1,YLIM[3]*y3_to_y1)

# move the 3rd y-axis to the left (0.0):
ax3.spines['right'].set_position(('axes', 0.0))

# set y-ticks, use np.arange for defined deltas
# add a small increment to the last ylim value
# to ensure that the last value will be a tick
ax1.set_yticks(np.arange(YLIM[0],YLIM[1]+0.001,10.0))
ax2.set_yticks(np.arange(YLIM[2],YLIM[3]+0.001,10.0))
ax3.set_yticks(np.arange(YLIM[2]*y3_to_y1,YLIM[3]*y3_to_y1+0.001, 2.0))

# for both letf-hand y-axis move the ticks to the outside:
ax1.get_yaxis().set_tick_params(direction='out')
ax3.get_yaxis().set_tick_params(direction='out')

plt.show()
Run Code Online (Sandbox Code Playgroud)

这导致了这个数字:

第二个数字

同样,这set_tick_params(direction='out')不适axisartist用于第一个例子.
有点反直觉,必须设置y1y3滴答'out'.因为y1,这是有道理的,因为y3你必须记住它是从右侧轴开始的.因此,'in'当轴向左移动时,这些刻度将出现在外部(使用默认设置).