不能设置双轴的位置 [英] Setting position of twin axes not possible

查看:23
本文介绍了不能设置双轴的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
a = np.array([1,2,3])
b = a 
ax1.plot(a,b)
ax2 = ax1.twinx()
ax2.set_position(matplotlib.transforms.Bbox([[0.125, 0.125], [0.9, 0.2]]))
c = np.array([4,5,6])
d = c
ax2.plot(c,d)
plt.show()

当我用 Python 2 运行它时,结果是:

When I run this with Python 2, it results in:

问题是当我尝试使用Python 3使用相同的代码时,我得到了这张图片:

The problem is when I try to use the same code using Python 3 I get this picture:

使用Python 3如何获得相同的结果?

How can I have the same result using Python 3?

推荐答案

这是一个错误,现已修复(因此它与 python 版本无关,而是与使用的 matplotlib 版本无关).您可以使用inset_axes而不是通常的子图.后者可能如下所示:

This was a bug, which has now been fixed (so it has nothing to do with the python version, but rather the matplotlib version in use). You could use an inset_axes instead of just a usual subplot. The latter could look like this:

import numpy as np
from matplotlib.transforms import Bbox
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111, label="first")
ax2 = fig.add_subplot(111, label="second")

ax2.set_position(Bbox([[0.125, 0.125], [0.9, 0.2]]))
ax1.get_shared_x_axes().join(ax1, ax2)
ax2.yaxis.tick_right()
ax2.tick_params(bottom=False, labelbottom=False)
ax2.set_facecolor("none")

a = np.array([1,2,3])
ax1.plot(a,a)

c = np.array([4,5,6])
ax2.plot(c,c)

plt.show()

这篇关于不能设置双轴的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆