How to pass Unicode title to matplotlib?

2024/9/20 1:35:52

Can't get the titles right in matplotlib: 'technologieën in °C' gives: technologieÃn in ÃC

Possible solutions already tried:

  • u'technologieën in °C' doesn't work
  • neither does: # -*- coding: utf-8 -*- at the beginning of the code-file.

Any solutions?

Answer

You need to pass in unicode text:

u'technologieën in °C'

Do make sure you use the # -*- coding: utf-8 -*- comment at the top, and make sure your text editor is actually using that codec. If your editor saves the file as Latin-1 encoded text, use that codec in the header, etc. The comment communicates to Python how to interpret your source file, especially when it comes to parsing string literals.

Alternatively, use escape codes for anything non-ASCII in your Unicode literals:

u'technologie\u00ebn in \u00b0C'

and avoid the issue of what codec to use in the first place.

I urge you to read:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

  • The Python Unicode HOWTO

  • Pragmatic Unicode by Ned Batchelder

before you continue.

Most fonts will support the °, but if you see a box displayed instead, then you have a font issue and need to switch to a font that supports the characters you are trying to display. For example, if Ariel supports your required characters, then use:

matplotlib.rc('font', family='Arial')

before plotting.

https://en.xdnf.cn/q/72165.html

Related Q&A

Cythonize but not compile .pyx files using setup.py

I have a Cython project containing several .pyx files. To distribute my project I would like to provide my generated .c files as recommended in the Cython documentation, to minimize problems with diffe…

How to clear matplotlib labels in legend?

Is there a way to clear matplotlib labels inside a graphs legend? This post explains how to remove the legend itself, but the labels themselves still remain, and appear again if you plot a new figure.…

Threading and Signals problem in PyQt

Im having some problems with communicating between Threads in PyQt. Im using signals to communicate between two threads, a Sender and a Listener. The sender sends messages, which are expected to be rec…

stopping a python thread using __del__

I have a threaded program in Python that works fine except that __del__ does not get called once the thread is running:class tt(threading.Thread):def __init__(self):threading.Thread.__init__(self)self.…

Python-docx: Is it possible to add a new run to paragraph in a specific place (not at the end)

I want to set a style to a corrected word in MS Word text. Since its not possible to change text style inside a run, I want to insert a new run with new style into the existing paragraph...for p in doc…

Chained QSortFilterProxyModels

Lets say I have a list variable datalist storing 10,000 string entities. The QTableView needs to display only some of these entities. Thats is why QTableView was assigned QSortFilterProxyModel that doe…

Python subprocess.popen() without waiting

Im using Python 3.4.2 on Windows. In script1.py Im doing this:myProc = subprocess.Popen([sys.executable, "script2.py", "argument"]) myProc.communicate()it works and call script2.py …

Python27.dll File Missing - Exception

I have downloaded my code from bit-bucket which was made by my group member. It contain all the frameworks and python script folder. But when I run this code on my system it generates the following err…

Download an Image Using Selenium Webdriver in Python

I am trying to download an image from a URL using Selenium Webdriver in Python. The site is protected by a login page, so cant just save the URL contents using requests. I am able to get text from the …

Can I turn off implicit Python unicode conversions to find my mixed-strings bugs?

When profiling our code I was surprised to find millions of calls toC:\Python26\lib\encodings\utf_8.py:15(decode)I started debugging and found that across our code base there are many small bugs, usual…