The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Python
 Updating properties of legend
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

daanwielens

Netherlands
6 Posts

Posted - 10/20/2022 :  08:28:29 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 2022 SR1 (9.9.0.255)
Operating System: Win10

Hi, I'm trying to update properties of a legend in a plot. I would like to remove the border of the legend (in GUI: Frame --> None), and I would like to position the legend automatically (like matplotlib's "best" location). Could you help me with these two questions?

Currently, my legend is constructed as:
lgnd = gl.label('Legend')
and I verified that its binding is correctly by moving it around:
lgnd.set_int('left', <value>)

YimingChen

1618 Posts

Posted - 10/20/2022 :  09:39:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Can you try the script below? You can put the legend at specified x and y values on the graph.

import os
import originpro as op

wks = op.new_sheet()
wks.from_file(os.path.join(op.path('e'), 'Samples', 'Graphing', 'Group.dat'))
graph = op.new_graph(template='scatter')
gl=graph[0]

# plot whole sheet as XY plot
plot = gl.add_plot(f'{wks.lt_range()}!(?,1:end)')

# group the plots and control plots setting in group
gl.group()
plot.colormap = 'Candy'
plot.shapelist = [3, 2, 1]
gl.rescale()

# Customize Legend 
lgnd = gl.label('Legend')
lgnd.set_int('showframe',0)
xto = gl.get_float('x.to') // x axis end value
yto = gl.get_float('y.to') // y axis end value
lgnd.set_float('x', xto - lgnd.get_float('dx') / 2) // legend center x position
lgnd.set_float('y', yto - lgnd.get_float('dy') / 2) // legend center y position


James

Edited by - YimingChen on 10/20/2022 09:57:45 AM
Go to Top of Page

daanwielens

Netherlands
6 Posts

Posted - 10/20/2022 :  10:27:00 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks for your quick reply. The showborder works perfectly. The positioning puts the legend within the graph, but it seems to be a fixed position. The idea of matplotlib's "best" location is that when data is located in the top-right corner of the graph, the legend will be moved automatically to a place where it does not overlap with any data. So my question is whether this automatic behavior is also possible within Origin. Thanks!
Go to Top of Page

YimingChen

1618 Posts

Posted - 10/20/2022 :  11:39:27 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Origin doesn't support the auto-positioning of the legend. You may write code to realize it. But from the document of matplotlib, it can be very slow for large amount of data.
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html

James
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000