python - Legend transparent to horizontal grid in matplotlib -


i'm working following class:

import numpy np import matplotlib matplotlib.use('qt4agg') import matplotlib.pyplot plt import matplotlib.ticker plticker  class matplotliv():      def __init__(self, basefilename, temperatures, length=none, width=none, area=none, title = '', ylim=none):         self.basefilename = basefilename         self.temperatures = temperatures         if length , width:             self.length = length             self.width = width             self.area = length*width*1e-5         else:             self.area = area         self.title = title         self.ylim = ylim          filenames = [("%s_%sk.txt" % (self.basefilename, str(temp)), temp) temp in self.temperatures]         self.rawdata = [(np.loadtxt(fname), temp) fname, temp in filenames]         self.colors = colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#e6ab02', '#a6761d', '#666666']          self.maxvaluerow = (0,0,0)      def plot(self):          self.fig = plt.figure()         self.ax1 = self.fig.add_subplot(111)         ax1 = self.ax1         ax1.tick_params(bottom='off')         ax1.xaxis.tick_top()         self.ax2 = ax1.twinx()         ax2 = self.ax2         self.ax3 = ax2.twiny()         ax3 = self.ax3         ax3.xaxis.tick_bottom()          ax1.set_xlabel("current / a")         ax1.xaxis.set_label_position('top')         ax1.set_ylabel("voltage / v")         ax2.set_ylabel("light intensity / arb. u.")         ax3.set_xlabel(r'current density / $\mathregular{acm^{-2}}$')         ax3.xaxis.set_label_position('bottom')          i, (datafile, label) in enumerate(self.rawdata):             self.checkmaxvalues(datafile)             ax1.plot( datafile[:,0], datafile[:,1], color=self.colors[i], label='%sk' % str(label))             ax2.plot( datafile[:,0], datafile[:,2], color=self.colors[i], label='%sk' % str(label), linewidth=2)           ax1.margins(x=0)          ax1.grid(true, axis='y')         ax3.grid(true)          start, end = ax1.get_xlim()          self.setaxesscale(ax1, ax2)         if self.ylim:             ax2.set_ylim(top=self.ylim)          ax3.set_xlim(start/self.area, end/self.area)         leg = ax2.legend(loc='upper left')          self.fig.suptitle(self.title, y=0.98, weight='bold')         self.fig.subplots_adjust(top=0.86)          loc = plticker.multiplelocator(base=20.0) # locator puts ticks @ regular intervals         ax3.xaxis.set_major_locator(loc)      def checkmaxvalues(self, data):         maxind = data.argmax(axis=0)[2]         if data[maxind][2] > self.maxvaluerow[2]:             self.maxvaluerow = data[maxind]      def setaxesscale(self, ax1, ax2):         yrange = ax1.get_ylim()         y1fraction = self.maxvaluerow[1]/yrange[1]         y2fraction = y1fraction - 0.02         ax2.set_ylim(top=self.maxvaluerow[2]/y2fraction)      def show(self):         plt.savefig(self.basefilename + '.pdf')                 plt.show() 

which can run sample code:

import matplotliv mpliv  ######## configuration  basefilename = "testdata" temperatures = (5,)  area = 1e-8  ######## end of configuration  liv = mpliv.matplotliv(basefilename, temperatures, area=area) liv.plot() liv.show() 

on file: http://pastebin.com/gmac3muu

the problem i'm experiencing legend transparent grid. oddly enough, vertical grid can see through legend box:

grid seen through legend

is bug? if not, how set legend not transparent?

the problem vertical grid on ax3, , legend on ax2, grid plotted after legend.

one way around pasted below (just section need modify). need plot legend on ax3, , explicitly tell lines , labels want.

    # make list lines plotting     l1 = []     l2 = []     i, (datafile, label) in enumerate(self.rawdata):         self.checkmaxvalues(datafile)         # give lines names (l1,l2)         l1+=ax1.plot( datafile[:,0], datafile[:,1], color=self.colors[i], label='%sk' % str(label))         l2+=ax2.plot( datafile[:,0], datafile[:,2], color=self.colors[i], label='%sk' % str(label), linewidth=2)      # define lines put in legend. if want l1 too, use lns = l1+l2     lns = l2     labs = [l.get_label() l in lns]      ax1.margins(x=0)      ax1.grid(true, axis='y')     ax3.grid(true)      start, end = ax1.get_xlim()      self.setaxesscale(ax1, ax2)     if self.ylim:         ax2.set_ylim(top=self.ylim)      ax3.set_xlim(start/self.area, end/self.area)     # set legend on ax3, not ax2     leg = ax3.legend(lns,labs,loc='upper left') 

enter image description here


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -