seaborn或matplotlib网格覆盖了地块中的线

时间:2015-09-02 14:48:31

标签: python matplotlib seaborn

这是我的(不完整的,我已经注释添加了数据本身)代码,这会产生一个有点令人困惑的情节,其中一行被网格覆盖但另一行没有。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pylab

sns.set_context("poster",font_scale=fs)
sns.set_style("darkgrid") # No grid lines
# sns.set_style({'legend.frameon': 'True'})
sns.set_style({'xtick.major.size':'0.0'})
c1,c2 = sns.color_palette("hls",2)#sns.color_palette("colorblind", 2)
a = sns.color_palette("BuGn_r")


# runs_plot = pd.DataFrame(runs.values+8.5)

# Plot just first state trajectory 
fig, ax1 = plt.subplots(1,sharey=True, sharex=True, figsize=(30,8))
ax1.plot((ground.values+6),label='Ground Truth',color=c1)
ax1.set_xlabel('Time [$s$]')
ax1.set_ylim(0,10)
ax1.set_ylabel('State [$\#$]')
for tl in ax1.get_yticklabels():
    tl.set_color(c1)

ax2 = ax1.twinx()
ax2.plot(0.4*signal_syn.values+1,color=c2,label='Emission Signal')
ax2.set_ylabel('Observations')
ax2.set_ylim(0,10)
# ax2.set_axisbelow(True)
for tl in ax2.get_yticklabels():
    tl.set_color(c2)

# ask matplotlib for the plotted objects and their labels
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2,ncol=5,loc='upper center', bbox_to_anchor=(0.5, -0.2))

plt.show()

产生

enter image description here

现在你可以看到,对于"地面真相"这条线由“暗网格”覆盖。 seaborn的选项(如上所示产生白色网格)。现在由于某种原因,网格不高于发射信号,而只是基本事实。

为什么会出现这种情况的想法?

2 个答案:

答案 0 :(得分:0)

所以这就是我最终要做的事情,它可能更像是一个黑客而不是一个真正的解决方案,但它确实有效。我只是移动了绘图元素,以便它们全部绘制在网格上方。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pylab

sns.set_context("poster",font_scale=fs)
sns.set_style("darkgrid") # No grid lines
# sns.set_style({'legend.frameon': 'True'})
sns.set_style({'xtick.major.size':'0.0'})
c1,c2 = sns.color_palette("hls",2)#sns.color_palette("colorblind", 2)
a = sns.color_palette("BuGn_r")


# runs_plot = pd.DataFrame(runs.values+8.5)

# Plot just first state trajectory 
fig, ax1 = plt.subplots(1,sharey=True, sharex=True, figsize=(30,8))
ax1.set_xlabel('Time [$s$]')
ax1.set_ylim(0,10)
ax1.set_ylabel('State [$\#$]')
for tl in ax1.get_yticklabels():
    tl.set_color(c1)

ax2 = ax1.twinx()
ax2.plot((ground.values+6),label='Ground Truth',color=c1)
ax2.plot(0.4*signal_syn.values+1,color=c2,label='Emission Signal')
ax2.set_ylabel('Observations')
ax2.set_ylim(0,10)
# ax2.set_axisbelow(True)
for tl in ax2.get_yticklabels():
    tl.set_color(c2)

# ask matplotlib for the plotted objects and their labels
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2,ncol=5,loc='upper center', bbox_to_anchor=(0.5, -0.2))

plt.show()

答案 1 :(得分:0)

好像是这个问题的答案:

Matplotlib: draw grid lines behind other graph elements

基本上是:Axis.set_axisbelow(True)