pylab中的顶轴和底轴(例如w /不同单位)(或左右)

时间:2009-04-15 12:57:28

标签: matplotlib plot axes

我正在尝试用pylab / matplotlib创建一个图,我有两组不同的x轴单位。所以我想要的情节是两个不同刻度的轴,一个在顶部,一个在底部。 (例如,一个有英里,一个有km左右。)

如下图所示(但我想要多个X轴,但这并不重要。)

Multiple axis with JpGraph

有人知道这是否可以用pylab进行?

2 个答案:

答案 0 :(得分:3)

这可能有点晚,但看看这样的事情会有所帮助:

http://matplotlib.sourceforge.net/examples/axes_grid/simple_axisline4.html

答案 1 :(得分:1)

如示例图片所示,可以在“原始”帧旁边添加多个轴,这可以通过使用twinx添加其他轴并配置其spines属性来实现。

我认为这与示例图片非常相似:

Figure 1

import matplotlib.pyplot as plt
import numpy as np

# Set font family
plt.rcParams["font.family"] = "monospace"

# Get figure, axis and additional axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twinx()
ax4 = ax1.twinx()

# Make space on the right for the additional axes
fig.subplots_adjust(right=0.6)

# Move additional axes to free space on the right
ax3.spines["right"].set_position(("axes", 1.2))
ax4.spines["right"].set_position(("axes", 1.4))

# Set axes limits
ax1.set_xlim(0, 7)
ax1.set_ylim(0, 10)
ax2.set_ylim(15, 55)
ax3.set_ylim(200, 600)
ax4.set_ylim(500, 750)

# Add some random example lines
line1, = ax1.plot(np.random.randint(*ax1.get_ylim(), 8), color="black")
line2, = ax2.plot(np.random.randint(*ax2.get_ylim(), 8), color="green")
line3, = ax3.plot(np.random.randint(*ax3.get_ylim(), 8), color="red")
line4, = ax4.plot(np.random.randint(*ax4.get_ylim(), 8), color="blue")

# Set axes colors
ax1.spines["left"].set_color(line1.get_color())
ax2.spines["right"].set_color(line2.get_color())
ax3.spines["right"].set_color(line3.get_color())
ax4.spines["right"].set_color(line4.get_color())

# Set up ticks and grid lines
ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax4.minorticks_on()
ax1.tick_params(direction="in", which="both", colors=line1.get_color())
ax2.tick_params(direction="in", which="both", colors=line2.get_color())
ax3.tick_params(direction="in", which="both", colors=line3.get_color())
ax4.tick_params(direction="in", which="both", colors=line4.get_color())
ax1.grid(axis='y', which='major')

plt.show()