Use unicode character in PyPlot

846 views
Skip to first unread message

Alex Dowling

unread,
Dec 31, 2015, 11:01:37 PM12/31/15
to julia-users
Hello,

I'd like to use a unicode character as a marker in plots created with PyPlot. Here is a minimal code example:

x = [ 0 1 2]
y = x
scatter(x,y,marker="\U0001F4A7")

which produces the following error:

ERROR: PyError (:PyObject_Call) <type 'exceptions.UnicodeEncodeError'>
UnicodeEncodeError('ascii', u'\U0001f4a7', 0, 1, 'ordinal not in range(128)')
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 3087, in scatter
    linewidths=linewidths, verts=verts, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6296, in scatter
    marker_obj = mmarkers.MarkerStyle(marker)
  File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 162, in __init__
    self.set_marker(marker)
  File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 236, in set_marker
    raise ValueError('Unrecognized marker style {}'.format(marker))

 [inlined code] from /home/adowling2/.julia/v0.4/PyCall/src/exception.jl:81
 in pycall at /home/adowling2/.julia/v0.4/PyCall/src/PyCall.jl:79
 in scatter at /home/adowling2/.julia/v0.4/PyPlot/src/PyPlot.jl:460

Interestingly, title("\U0001F4A7") half works. It adds a title with a box in place of the unicode character. I suspect this is because the default font does not include the character.

My questions:
  1. Any suggestions regarding the error above?
  2. Any suggestions to change the font used for the title command?
Thanks,
Alex

Steven G. Johnson

unread,
Jan 1, 2016, 9:09:19 AM1/1/16
to julia-users
See e.g. here on changing the font: https://github.com/stevengj/PyPlot.jl/issues/114

Probably you should file a Matplotlib issue, since I don't think this is Julia-specific.

Andreas Lobinger

unread,
Jan 1, 2016, 12:17:11 PM1/1/16
to julia-users
Hello c,


On Friday, January 1, 2016 at 5:01:37 AM UTC+1, Alex Dowling wrote:
I'd like to use a unicode character as a marker in plots created with PyPlot. Here is a minimal code example:

x = [ 0 1 2]
y = x
scatter(x,y,marker="\U0001F4A7")

which produces the following error:


 
My questions:
  1. Any suggestions regarding the error above?

as far as i can see the mathplotlib documentation about scatter and entries in marker (http://matplotlib.org/api/markers_api.html#module-matplotlib.markers) there are only certain entries in marker allowed and no free text. If you have a limited set of x,y you could use pyplot.text which handles local coordinates and unicode text.
 

Alex Dowling

unread,
Jan 2, 2016, 9:32:17 PM1/2/16
to julia-users
Andreas and Steve,

Thank you for the help. I'm now using text and figured out how to get matplotlib to use the correct font.

Naturally, I'd like to add the unicode symbols to the legend I'm generating with PyPlot. This stackoverflow post suggests a legend handler goes exist for text objects, and I'll need to implement one. The post gives some Python code. How exactly would I go about implementing this custom handler in Julia? I suspect this is a more general PyCall question. If this group isn't an appropriate place for my question, any recommends on where I should ask it?

Thanks,
Alex
Reply all
Reply to author
Forward
0 new messages