Changeset 4298


Ignore:
Timestamp:
Feb 12, 2020 5:39:55 PM (4 years ago)
Author:
toby
Message:

put data items into legend, but make that optional; fix publication plot to work independent of legend status; offer publication plot with sqrt(I)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIplot.py

    r4294 r4298  
    201201    Angstr = chr(0x00c5)
    202202    Pwrm1 = chr(0x207b)+chr(0x0b9)
     203# misc global vars
    203204nxs = np.newaxis
    204205plotDebug = False
    205206timeDebug = GSASIIpath.GetConfigValue('Show_timing',False)
     207obsInCaption = True # include the observed, calc,... items in the plot caption (PlotPatterns)
    206208
    207209#matplotlib 2.0.x dumbed down Paired to 16 colors -
     
    18231825            if msg: msg += '\n'
    18241826            msg += " * only when a single histogram is plotted"
    1825         if Page.plotStyle['logPlot'] or Page.plotStyle['sqrtPlot']:
     1827        if Page.plotStyle['logPlot']:
    18261828            if msg: msg += '\n'
    1827             msg += " * only when the intensity scale is linear (not log or sqrt)"
     1829            msg += " * only when the intensity scale is linear/sqrt (not log)"
    18281830        if G2frame.Weight:
    18291831            if msg: msg += '\n'
     
    19191921        elif event.key == 'r' and not G2frame.SinglePlot:
    19201922            Page.plotStyle['Offset'][1] += 1.
    1921         elif event.key == 'o' and not G2frame.SinglePlot:
    1922             G2frame.Cmax = 1.0
    1923             G2frame.Cmin = 0.0
    1924             Page.plotStyle['Offset'] = [0,0]
     1923        elif event.key == 'o':
     1924            if G2frame.SinglePlot and not G2frame.Contour:
     1925                global obsInCaption # include the observed, calc,... items in the plot caption (PlotPatterns)
     1926                obsInCaption = not obsInCaption
     1927            elif not G2frame.SinglePlot:
     1928                G2frame.Cmax = 1.0
     1929                G2frame.Cmin = 0.0
     1930                Page.plotStyle['Offset'] = [0,0]
    19251931        elif event.key == 'c' and 'PWDR' in plottype:
    19261932            newPlot = True
     
    20142020        else:
    20152021            #print('no binding for key',event.key)
    2016             #GSASIIpath.IPyBreak()
    20172022            return
    20182023        wx.CallAfter(PlotPatterns,G2frame,newPlot=newPlot,plotType=plottype,extraKeys=extraKeys)
     
    27202725            Plot.set_title(Title)
    27212726        Page.canvas.draw()
     2727    def incCptn(string):
     2728        '''Adds a underscore to "hide" a MPL object from the legend if
     2729        obsInCaption is False
     2730        '''
     2731        if obsInCaption:
     2732            return string
     2733        else:
     2734            return '_'+string
    27222735
    27232736    #=====================================================================================
     
    27842797        G2frame.Cmin = 0.0
    27852798        G2frame.Cmax = 1.0
    2786 #        Page.canvas.mpl_connect('key_press_event', OnPlotKeyPress)
    27872799        Page.canvas.mpl_connect('motion_notify_event', OnMotion)
    27882800        Page.canvas.mpl_connect('pick_event', OnPick)
     
    28372849                'a: add magnification region','b: toggle subtract background',
    28382850                'c: contour on','g: toggle grid','m: toggle multidata plot',
    2839                 'n: toggle log(I)','q: toggle q plot','s: toggle sqrt plot',
     2851                'n: toggle log(I)',]
     2852            if obsInCaption:
     2853                Page.Choice += ['o: remove obs, calc,... from legend',]
     2854            else:
     2855                Page.Choice += ['o: add obs, calc,... to legend',]
     2856            Page.Choice += ['q: toggle q plot','s: toggle sqrt plot',
    28402857                't: toggle d-spacing plot','w: toggle (Io-Ic)/sig plot',
    28412858                '+: no selection']
     
    32153232                    else:
    32163233                        DZ = (xye[1]-xye[3])*np.sqrt(wtFactor*xye[2])
    3217                     DifLine = Plot1.plot(X,DZ,colors[3],picker=1.,label='_diff')                    #(Io-Ic)/sig(Io)
     3234                    DifLine = Plot1.plot(X,DZ,colors[3],picker=1.,label=incCptn('diff'))                    #(Io-Ic)/sig(Io)
    32183235                    Plot1.axhline(0.,color='k')
    32193236
     
    32213238                    if 'PWDR' in plottype:
    32223239                        Plot.set_yscale("log",nonposy='mask')
    3223                         Plot.plot(X,Y,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs')
     3240                        Plot.plot(X,Y,colors[0]+pP,picker=3.,clip_on=Clip_on,label=incCptn('obs'))
    32243241                        if G2frame.SinglePlot or G2frame.plusPlot:
    3225                             Plot.plot(X,Z,colors[1],picker=False,label='_calc')
     3242                            Plot.plot(X,Z,colors[1],picker=False,label=incCptn('calc'))
    32263243                            if G2frame.plusPlot:
    3227                                 Plot.plot(X,W,colors[2],picker=False,label='_bkg')     #background
     3244                                Plot.plot(X,W,colors[2],picker=False,label=incCptn('bkg'))     #background
    32283245                    elif plottype in ['SASD','REFD']:
    32293246                        Plot.set_xscale("log",nonposx='mask')
     
    32353252                            else:
    32363253                                Plot.errorbar(X,YB,yerr=Sample['Scale'][0]*np.sqrt(1./(Pattern[0]['wtFactor']*xye[2])),
    3237                                     ecolor=colors[0],picker=3.,clip_on=Clip_on,label='_obs')
     3254                                    ecolor=colors[0],picker=3.,clip_on=Clip_on,label=incCptn('obs'))
    32383255                        else:
    3239                             Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs')
    3240                         Plot.plot(X,W,colors[1],picker=False,label='_bkg')     #const. background
    3241                         Plot.plot(X,ZB,colors[2],picker=False,label='_calc')
     3256                            Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label=incCptn('obs'))
     3257                        Plot.plot(X,W,colors[1],picker=False,label=incCptn('bkg'))     #const. background
     3258                        Plot.plot(X,ZB,colors[2],picker=False,label=incCptn('calc'))
    32423259                else:  # not logPlot
    32433260                    if G2frame.SubBack:
    32443261                        if 'PWDR' in plottype:
    3245                             ObsLine = Plot.plot(Xum,Y,colors[0]+pP,picker=False,clip_on=Clip_on,label='_obs-bkg')  #Io-Ib
     3262                            ObsLine = Plot.plot(Xum,Y,colors[0]+pP,picker=False,clip_on=Clip_on,label=incCptn('obs-bkg'))  #Io-Ib
    32463263                            if np.any(Z):       #only if there is a calc pattern
    3247                                 CalcLine = Plot.plot(X,Z-W,colors[1],picker=False,label='_calc-bkg')               #Ic-Ib
     3264                                CalcLine = Plot.plot(X,Z-W,colors[1],picker=False,label=incCptn('calc-bkg'))               #Ic-Ib
    32483265                        else:
    3249                             Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs')
    3250                             Plot.plot(X,ZB,colors[2],picker=False,label='_calc')
     3266                            Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label=incCptn('obs'))
     3267                            Plot.plot(X,ZB,colors[2],picker=False,label=incCptn('calc'))
    32513268                    else:
    32523269                        if 'PWDR' in plottype:
    3253                             ObsLine = Plot.plot(Xum,Y,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs')    #Io
    3254                             CalcLine = Plot.plot(X,Z,colors[1],picker=False,label='_calc')                 #Ic
     3270                            ObsLine = Plot.plot(Xum,Y,colors[0]+pP,picker=3.,clip_on=Clip_on,label=incCptn('obs'))    #Io
     3271                            CalcLine = Plot.plot(X,Z,colors[1],picker=False,label=incCptn('calc'))                 #Ic
    32553272                        else:
    3256                             Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label='_obs')
    3257                             Plot.plot(X,ZB,colors[2],picker=False,label='_calc')
     3273                            Plot.plot(X,YB,colors[0]+pP,picker=3.,clip_on=Clip_on,label=incCptn('obs'))
     3274                            Plot.plot(X,ZB,colors[2],picker=False,label=incCptn('calc'))
    32583275                    if 'PWDR' in plottype and (G2frame.SinglePlot and G2frame.plusPlot):
    3259                         BackLine = Plot.plot(X,W,colors[2],picker=False,label='_bkg')                 #Ib
    3260                         if not G2frame.Weight and np.any(Z): 
    3261                             DifLine = Plot.plot(X,D,colors[3],picker=1.,label='_diff')                 #Io-Ic
     3276                        BackLine = Plot.plot(X,W,colors[2],picker=False,label=incCptn('bkg'))                 #Ib
     3277                        if not G2frame.Weight and np.any(Z):
     3278                            DifLine = Plot.plot(X,D,colors[3],picker=1.,label=incCptn('diff'))                 #Io-Ic
    32623279                    Plot.axhline(0.,color='k',label='_zero')
    32633280                Page.SetToolTipString('')
     
    33693386                handles = [labels[item] for item in labels]
    33703387                legends = list(labels.keys())
    3371                 Plot.legend(handles,legends,title='Phases',loc='best')
    3372            
     3388                if len(Phases) and obsInCaption:
     3389                    Plot.legend(handles,legends,title='Phases & Data',loc='best')
     3390                else:
     3391                    Plot.legend(handles,legends,title='Data',loc='best')
     3392                   
    33733393    if G2frame.Contour:
    33743394        time0 = time.time()
     
    34903510            if 'mag' in lbl:
    34913511                pass
    3492             elif lbl[1:] in plotOpt['lineList']:
     3512            elif lbl[1:] in plotOpt['lineList']: # item not in legend
    34933513                if lbl[1:] in plotOpt['colors']: continue
    34943514                plotOpt['colors'][lbl[1:]] = MPL2rgba(l.get_color())
    34953515                plotOpt['legend'][lbl[1:]] = False
     3516            elif lbl in plotOpt['lineList']:
     3517                if lbl in plotOpt['colors']: continue
     3518                plotOpt['colors'][lbl] = MPL2rgba(l.get_color())
     3519                plotOpt['legend'][lbl] = True
    34963520            elif l in Page.tickDict.values():
    34973521                plotOpt['phaseList'] .append(lbl)
     
    36033627        fp.write('@{}axis tick major {}\n'.format('y',yticks[1]-yticks[0]))
    36043628        fp.write('@{}axis ticklabel char size {}\n'.format('x',0)) # turns off axis labels
    3605         ylbl = Plot.get_ylabel().replace('$','')
     3629        if 'sqrt' in Plot.yaxis.get_label().get_text():
     3630            ylbl = 'sqrt(Intensity)' # perhaps there is a way to get the symbol in xmgrace but I did not find it
     3631        else:
     3632            ylbl = 'Intensity'
    36063633        fp.write('@{0}axis label "{1}"\n@{0}axis label char size {2}\n'.format(
    36073634            'y',ylbl,float(plotOpt['labelSize'])/8.))
     
    36413668    # plot data
    36423669        for l in Plot.lines:
    3643             lbl = l.get_label()
    3644             if lbl[1:] not in ('obs','calc','bkg','zero','diff'): continue
    3645             c = plotOpt['colors'].get(lbl[1:],l.get_color())
     3670            if l.get_label() in ('obs','calc','bkg','zero','diff'):
     3671                lbl = l.get_label()
     3672            elif l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
     3673                lbl = l.get_label()[1:]
     3674            else:
     3675                continue
     3676            c = plotOpt['colors'].get(lbl,l.get_color())
    36463677            gc = ClosestColorNumber(c)
    36473678            if sum(c) == 4.0: # white on white, skip
     
    36523683            mkwid = l.get_mew()
    36533684            glinetyp = 1
    3654             if lbl[1:] == 'obs':
     3685            if lbl == 'obs':
    36553686                obsartist = l
    36563687                gsiz = float(plotOpt['markerSiz'])/8.
     
    36643695                gmw = 0
    36653696                lineWid = float(plotOpt['lineWid'])
    3666             if plotOpt['legend'].get(lbl[1:]):
    3667                 glbl = lbl[1:]
     3697            if plotOpt['legend'].get(lbl):
     3698                glbl = lbl
    36683699            else:
    36693700                glbl = ""
    36703701            datnum += 1
    36713702            fp.write("@type xy\n")
    3672             if lbl[1:] == 'zero':
     3703            if lbl == 'zero':
    36733704                fp.write("{} {}\n".format(Plot.get_xlim()[0],0))
    36743705                fp.write("{} {}\n".format(Plot.get_xlim()[1],0))
     
    38073838            if plotOpt['legend'].get(lbl[1:]):
    38083839                legends.append((InameDict[lbl[1:]],lbl[1:]))
    3809             if 'mag' in lbl:
    3810                 pass
    3811             elif lbl[1:] in ('obs','calc','bkg','zero','diff'):
    3812                 if lbl[1:] == 'obs':
    3813                     x = l.get_xdata()
    3814                     valueList.append(x)
    3815                     zerovals = (x.min(),x.max())
    3816                     gsiz = 5*float(plotOpt['markerSiz'])/8.
    3817                     marker = plotOpt['markerSym']
    3818                     gsym = igor_symbols.get(marker,12)
    3819                     gmw = float(plotOpt['markerWid'])
    3820                     markerSettings.append(
    3821                         'mode({0})=3,marker({0})={1},msize({0})={2},mrkThick({0})={3}'
    3822                         .format('Intensity',gsym,gsiz,gmw))
    3823                 else:
    3824                     markerSettings.append(
    3825                         'mode({0})=0, lsize({0})={1}'
    3826                         .format(InameDict[lbl[1:]],plotOpt['lineWid']))
    3827                 c = plotOpt['colors'].get(lbl[1:],l.get_color())
    3828                 #if sum(c) == 4.0: continue
    3829                 Icolor[InameDict[lbl[1:]]] = [j*65535 for j in c[0:3]]
    3830                 if lbl[1:] != 'zero':
    3831                     valueList.append(l.get_ydata())
     3840            if l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
     3841                lbl = l.get_label()[1:]
     3842            if plotOpt['legend'].get(lbl) and lbl in InameDict:
     3843                legends.append((InameDict[lbl],lbl))
     3844            if lbl == 'obs':
     3845                x = l.get_xdata()
     3846                valueList.append(x)
     3847                zerovals = (x.min(),x.max())
     3848                gsiz = 5*float(plotOpt['markerSiz'])/8.
     3849                marker = plotOpt['markerSym']
     3850                gsym = igor_symbols.get(marker,12)
     3851                gmw = float(plotOpt['markerWid'])
     3852                markerSettings.append(
     3853                    'mode({0})=3,marker({0})={1},msize({0})={2},mrkThick({0})={3}'
     3854                    .format('Intensity',gsym,gsiz,gmw))
     3855            elif lbl in ('calc','bkg','zero','diff'):
     3856                markerSettings.append(
     3857                    'mode({0})=0, lsize({0})={1}'
     3858                    .format(InameDict[lbl],plotOpt['lineWid']))
     3859            else:
     3860                continue
     3861            c = plotOpt['colors'].get(lbl,l.get_color())
     3862            #if sum(c) == 4.0: continue
     3863            Icolor[InameDict[lbl]] = [j*65535 for j in c[0:3]]
     3864            if lbl != 'zero':
     3865                valueList.append(l.get_ydata())
    38323866        valueList.append(Pattern[1][5]*np.sqrt(Pattern[1][2]))
    38333867        # invert lists into columns, use iterator for all values
     
    38853919        fp.write(' {0} 0.0\n {1} 0.0\n'.format(*zerovals))
    38863920        fp.write('END\nX AppendToGraph Zero vs ZeroX\n')
     3921        if 'sqrt' in Plot.yaxis.get_label().get_text():
     3922            ylabel = '\u221AIntensity'           
     3923        else:
     3924            ylabel = 'Intensity'
    38873925        fp.write('''X //  ***   add axis labels and position them
    38883926X Label left "{1}"
     
    38903928X Label bottom "{0}"
    38913929X ModifyGraph lblPosMode=0,lblPos(Res_left)=84
    3892 '''.format("2Θ","Intensity","∆/σ"))
     3930'''.format("2Θ",ylabel,"∆/σ"))
    38933931        fp.write('''X //  ***   set display limits.
    38943932X SetAxis left {2}, {3}
     
    39854023        tickpos = {}
    39864024        for i,l in enumerate(Plot.lines):
    3987             lbl = l.get_label()
     4025            if l.get_label() in ('obs','calc','bkg','zero','diff'):
     4026                lbl = l.get_label()
     4027            elif l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
     4028                lbl = l.get_label()[1:]
     4029            else:
     4030                lbl = l.get_label()
    39884031            if 'mag' in lbl:
    39894032                pass
    3990             elif lbl[1:] in ('obs','calc','bkg','zero','diff'):
    3991                 if lbl[1:] == 'obs':
     4033            elif lbl in ('obs','calc','bkg','zero','diff'):
     4034                if lbl == 'obs':
    39924035                    lblList.append('x')
    39934036                    valueList.append(l.get_xdata())
    3994                 c = plotOpt['colors'].get(lbl[1:],l.get_color())
     4037                c = plotOpt['colors'].get(lbl,l.get_color())
    39954038                if sum(c) == 4.0: continue
    39964039                lblList.append(lbl)
     
    41494192    gsizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Include in legend'),0,wx.ALL)
    41504193    for lbl in list(plotOpt['lineList']) + list(plotOpt['phaseList'] ):
     4194        if lbl not in plotOpt['legend']:
     4195            plotOpt['legend'][lbl] = False
    41514196        ch = G2G.G2CheckBox(dlg,'',plotOpt['legend'],lbl,RefreshPlot)
    41524197        gsizer.Add(ch,0,wx.ALL|wx.ALIGN_CENTER)
     
    41554200    for lbl in list(plotOpt['lineList']) + list(plotOpt['phaseList']):
    41564201        import  wx.lib.colourselect as csel
     4202        if lbl not in  plotOpt['colors']:
     4203            plotOpt['colors'][lbl] = (0.5, 0.5, 0.5, 1)
    41574204        color = wx.Colour(*[int(255*i) for i in plotOpt['colors'][lbl]])
    41584205        b = csel.ColourSelect(dlg, -1, '', color)
     
    42494296    obsartist = None
    42504297    for i,l in enumerate(Plot.lines):
    4251         lbl = l.get_label()
     4298        if l.get_label() in ('obs','calc','bkg','zero','diff'):
     4299            lbl = l.get_label()
     4300        elif l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
     4301            lbl = l.get_label()[1:]
     4302        else:
     4303            lbl = l.get_label()
    42524304        if 'mag' in lbl:
    42534305            ax0.axvline(l.get_data()[0][0],color='0.5',dashes=(1,1))
    4254         elif lbl[1:] in ('obs','calc','bkg','zero','diff'):
     4306        elif lbl in ('obs','calc','bkg','zero','diff'):
    42554307            marker = l.get_marker()
    42564308            lineWid = l.get_lw()
    42574309            siz = l.get_markersize()
    42584310            mew = l.get_mew()
    4259             if lbl[1:] == 'obs':
     4311            if lbl == 'obs':
    42604312                obsartist = l
    42614313                siz = float(plotOpt['markerSiz'])
     
    42644316            else:
    42654317                lineWid = float(plotOpt['lineWid'])
    4266             c = plotOpt['colors'].get(lbl[1:],l.get_color())
     4318            c = plotOpt['colors'].get(lbl,l.get_color())
    42674319            if sum(c) == 4.0: continue
    4268             if plotOpt['legend'].get(lbl[1:]):
    4269                 uselbl = lbl[1:]
     4320            if plotOpt['legend'].get(lbl):
     4321                uselbl = lbl
    42704322            else:
    4271                 uselbl = lbl
    4272             if lbl[1:] == 'zero':
     4323                uselbl = '_'+lbl
     4324            if lbl == 'zero':
    42734325                art = [ax0.axhline(0.,color=c,
    42744326                     lw=lineWid,label=uselbl,ls=l.get_ls(),
     
    42794331                     marker=marker,ms=siz,mew=mew,
    42804332                     )
    4281             if plotOpt['legend'].get(lbl[1:]):
     4333            if plotOpt['legend'].get(lbl):
    42824334                legLbl.append(uselbl)
    42834335                legLine.append(art[0])
  • trunk/makeBat.py

    r4266 r4298  
    1313
    1414'''
     15from __future__ import division, print_function
    1516version = "$Id$"
    1617# creates Windows files to aid in running GSAS-II
Note: See TracChangeset for help on using the changeset viewer.