matplotlib:添加第二个具有透明背景的axes()吗? [英] matplotlib: adding second axes() with transparent background?

查看:228
本文介绍了matplotlib:添加第二个具有透明背景的axes()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义数据

x = np.linspace(0,2*np.pi,100)
y = 2*np.sin(x)

情节

fig = plt.figure()
ax = plt.axes()
fig.add_subplot(ax)
ax.plot(x,y)

添加第二条轴

newax = plt.axes(axisbg='none')

请给我ValueError: Unknown element o,即使它执行与我将要描述的内容相同的操作.我还可以看到这样做(做同样的事情,没有错误):

Gives me ValueError: Unknown element o, even though it does the same thing as what I am about to describe. I can also see that this works (no error) to do the same thing:

newax = plt.axes()
fig.add_subplot(newax)
newax.set_axis_bgcolor('none')

但是,它会使原始图形的背景颜色变成灰色"(或任何图形背景)吗?我不明白,因为我认为这将使newax透明,但图中的轴和框除外.即使我切换顺序,也一样:

However, it turns the background color of the original figure "gray" (or whatever the figure background is)? I don't understand, as I thought this would make newax transparent except for the axes and box around the figure. Even if I switch the order, same thing:

plt.close('all')
fig = plt.figure()
newax = plt.axes()
fig.add_subplot(newax)
newax.set_axis_bgcolor('none')
ax = plt.axes()
fig.add_subplot(ax)
ax.plot(x,y)

这是令人惊讶的,因为我认为一个背景会覆盖另一个背景,但是无论哪种情况,它都是可见的newax背景(或者至少是我看到的颜色).

This is surprising because I thought the background of one would be overlaid on the other, but in either case it is the newax background that appears to be visible (or at least this is the color I see).

这是怎么回事?

推荐答案

您实际上并没有添加新轴.

You're not actually adding a new axes.

Matplotlib正在检测到该位置已经存在一个绘图,然后将其返回,而不是返回新的轴对象.

Matplotlib is detecting that there's already a plot in that position and returning it instead of a new axes object.

(亲自检查.axnewax将是同一对象.)

(Check it for yourself. ax and newax will be the same object.)

您可能不想这么做,但这是您要这样做的方式.

There's probably not a reason why you'd want to, but here's how you'd do it.

(另外,请勿先调用newax = plt.axes(),然后再调用fig.add_subplot(newax),这是您做两次相同的事情.)

(Also, don't call newax = plt.axes() and then call fig.add_subplot(newax) You're doing the same thing twice.)

使用较新的(> = 1.2,我认为是?)matplotlib版本,您可以通过对 kwarg使用fig.add_subplot来完成与以下示例相同的操作.例如. newax = fig.add_subplot(111, label='some unique string')

With newer (>=1.2, I think?) versions of matplotlib, you can accomplish the same thing as the example below by using the label kwarg to fig.add_subplot. E.g. newax = fig.add_subplot(111, label='some unique string')

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

# If you just call `plt.axes()` or equivalently `fig.add_subplot()` matplotlib  
# will just return `ax` again. It _won't_ create a new axis unless we
# call fig.add_axes() or reset fig._seen
newax = fig.add_axes(ax.get_position(), frameon=False)

ax.plot(range(10), 'r-')
newax.plot(range(50), 'g-')
newax.axis('equal')

plt.show()

当然,这看起来很糟糕,但这就是您要的...

Of course, this looks awful, but it's what you're asking for...

我从您先前的问题中猜测您仅想添加第二个x轴?如果是这样,那是完全不同的事情.

I'm guessing from your earlier questions that you just want to add a second x-axis? If so, this is a completely different thing.

如果要链接y轴,请执行以下操作(有点冗长...):

If you want the y-axes linked, then do something like this (somewhat verbose...):

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
newax = ax.twiny()

# Make some room at the bottom
fig.subplots_adjust(bottom=0.20)

# I'm guessing you want them both on the bottom...
newax.set_frame_on(True)
newax.patch.set_visible(False)
newax.xaxis.set_ticks_position('bottom')
newax.xaxis.set_label_position('bottom')
newax.spines['bottom'].set_position(('outward', 40))

ax.plot(range(10), 'r-')
newax.plot(range(21), 'g-')

ax.set_xlabel('Red Thing')
newax.set_xlabel('Green Thing')

plt.show()

如果您想拥有一个隐藏的,未链接的y轴和一个全新的x轴,则可以执行以下操作:

If you want to have a hidden, unlinked y-axis, and an entirely new x-axis, then you'd do something like this:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)

newax = fig.add_axes(ax.get_position())
newax.patch.set_visible(False)
newax.yaxis.set_visible(False)

for spinename, spine in newax.spines.iteritems():
    if spinename != 'bottom':
        spine.set_visible(False)

newax.spines['bottom'].set_position(('outward', 25))

ax.plot(range(10), 'r-')

x = np.linspace(0, 6*np.pi)
newax.plot(x, 0.001 * np.cos(x), 'g-')

plt.show()

请注意,在newax上绘制的任何内容的y轴值都不会显示.

Note that the y-axis values for anything plotted on newax are never shown.

如果需要,您甚至可以更进一步,并具有独立的x和y轴(我不太确定它的要点是什么,但是看起来很整洁……):

If you wanted, you could even take this one step further, and have independent x and y axes (I'm not quite sure what the point of it would be, but it looks neat...):

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2, right=0.85)

newax = fig.add_axes(ax.get_position())
newax.patch.set_visible(False)

newax.yaxis.set_label_position('right')
newax.yaxis.set_ticks_position('right')

newax.spines['bottom'].set_position(('outward', 35))

ax.plot(range(10), 'r-')
ax.set_xlabel('Red X-axis', color='red')
ax.set_ylabel('Red Y-axis', color='red')

x = np.linspace(0, 6*np.pi)
newax.plot(x, 0.001 * np.cos(x), 'g-')

newax.set_xlabel('Green X-axis', color='green')
newax.set_ylabel('Green Y-axis', color='green')


plt.show()

您还可以在图的底部添加一个额外的书脊.有时这会更容易,尤其是如果您不希望出现滴答声或数字内容的话.不要过多地插入我自己的答案之一,但是这里有一个示例:

You can also just add an extra spine at the bottom of the plot. Sometimes this is easier, especially if you don't want ticks or numerical things along it. Not to plug one of my own answers too much, but there's an example of that here: How do I plot multiple X or Y axes in matplotlib?

最后,请务必查看如果您希望通过特定的转换将不同的x和y轴链接起来,请使用寄生轴示例" .

这篇关于matplotlib:添加第二个具有透明背景的axes()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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