Python Matplotlib-双Y轴图中的脊柱着色问题

时间:2018-11-07 05:22:06

标签: python matplotlib

我正在编写一个脚本来通过matplotlib绘制图,下面的代码类似于我的原始脚本,该脚本重现了我所遇到的问题。

def func(ax, data, color, position):    # A function for plotting
    ax.plot(data[0], data[1], color=color)
    ax.spines[position].set_color(color)

fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]

ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')

ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')

plt.show()

预期图应同时对y轴进行着色,但是,仅右脊柱被着色,如下所示。 enter image description here

放大左脊椎时,您会在y轴周围找到一个红色阴影,这意味着我的彩色脊椎被另一根脊椎覆盖了,如何仅通过修改func()来解决此问题?

3 个答案:

答案 0 :(得分:1)

在执行以下代码ax_1 = ax.twinx()之后,似乎会像其他刺一样被创建,所以我找到了一个“愚蠢”的解决方案:

def func(ax, data, color, position):    # A function for plotting
    ax.plot(data[0], data[1], color=color)
    ax.spines[position].set_color(color)

    if position == 'left':
        other = 'right'
    elif position == 'right':
        other = 'left'
    ax.spines[other].set_color('None')

结果:

enter image description here

这可以解决我的问题,但是我仍然愿意接受其他漂亮的解决方案。

答案 1 :(得分:1)

twinx不仅“缠绕” y轴脊,还“缠绕”了所有其他三个脊椎。因此,左侧的红色脊柱基本上已经透支了(就像您已经在自己的答案中识别出一样)。您无需color将其None设置为set_visible(),而是可以Falsedef func(ax, data, color, position): # A function for plotting ax.plot(data[0], data[1], color=color) ax.spines[position].set_color(color) for pos in ['left', 'right']: if pos != position: ax.spines[pos].set_visible(False) 进行可见性,与第18-22行{{3}相比,这似乎是首选方法}。

所以:

   var app = document.getElementById('root');

   var container = document.createElement('div');
   container.setAttribute('class', 'container');
   container.id = 'divid';

    app.appendChild(container);

    var request = new XMLHttpRequest();
    request.open('GET', 'https://url of the api', true);
    request.onload = function () {
    var data = JSON.parse(this.response);
    if (request.status >= 200 && request.status < 400) {
        var image;
        for(var i = 0; i < data.length; i++){
        var pic = document.createElement('img');
        pic.src =  data.image;
        container.appendChild(pic);
     }  

  } else {
    var errorMessage = document.createElement('marquee');
    errorMessage.textContent = `Gah, it's not working!`;
    app.appendChild(errorMessage);
  }
}
request.send();

答案 2 :(得分:0)

我要从以下链接中找到来自matplotlib文档的问题答案:https://matplotlib.org/api/spines_api.html

这是可以使用的功能:

def make_patch_spines_invisible(ax):
ax.set_frame_on(True)
ax.patch.set_visible(False)
for sp in ax.spines.values():
    sp.set_visible(False)

并且,下面是使用它的示例:

import matplotlib.pyplot as plt


def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)


fig, host = plt.subplots()
fig.subplots_adjust(right=0.75)

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

# Offset the right spine of par2.  The ticks and label have already been
# placed on the right by twinx above.
par2.spines["right"].set_position(("axes", 1.2))
# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible.  First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(par2)
# Second, show the right spine.
par2.spines["right"].set_visible(True)

p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity")

host.set_xlim(0, 2)
host.set_ylim(0, 2)
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

host.yaxis.label.set_color(p1.get_color())
par1.yaxis.label.set_color(p2.get_color())
par2.yaxis.label.set_color(p3.get_color())

tkw = dict(size=4, width=1.5)
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
host.tick_params(axis='x', **tkw)

lines = [p1, p2, p3]

host.legend(lines, [l.get_label() for l in lines])

plt.show()

这将创建以下情节:

Plot multiple Y axes with different spine colors