0

Here is my (incomplete, I have note added the data itself) code, which produces a somewhat confusing plot, where one line is covered by the grid but the other not.

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()

which produces

enter image description here

now and you can probably see, that for the "Ground Truth" the line is covered by the 'darkgrid' option of the seaborn (which produces a white grid as seen above). Now for some reason the grid is not above the emission signal but only the ground truth.

Any ideas for why this might be?

Astrid
  • 1,846
  • 4
  • 26
  • 48
  • Unfortunately, my firewall doesn't allow me to see the image. However, I've found with matplotlib that the last thing plotted can overwrite prior things plotted, which is what it sounds like your problem is. – mauve Sep 02 '15 at 15:11

2 Answers2

0

So this is what I ended up doing, it is probably more of a hack than an actual solution, but it works. I just moved the plotting elements so that they're all plotted above the grid.

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()
Astrid
  • 1,846
  • 4
  • 26
  • 48
0

Seems like the answer is in this question:

Matplotlib: draw grid lines behind other graph elements

And it is basically: Axis.set_axisbelow(True)

T. Novais
  • 79
  • 8
  • Links are great, but it would be even more helpful if you could summarize a possible way to correct the problem outlined in the question. – Shanteshwar Inde May 06 '19 at 12:57