Skip to content

Creating figure resets locale #6706

Closed
Closed

Description

@skjaeve

I have an issue where I set the locale of the python script with locale.setlocale(). When I then create a figure, the locale is reset to the default.

I have trouble pinning it down exactly. On my computer (OpenSuse Tumbleweed) it happens with Matplotlib from Git, but not with system-supplied matplotlib. On a CentOS 6 running Miniconda python, it happens with Matplotlib 1.5.1 supplied with Miniconda.

MWE:

from __future__ import print_function
import locale
import sys
print ('Python version:', sys.__version__)
print ("Initial locale:", locale.getlocale())
print ('Changing locale to en_US')
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
print ("New locale:", locale.getlocale())
import matplotlib
print ("Matplotlib version:", matplotlib.__version__)
print ("Locale after importing matplotlib:", locale.getlocale())
import matplotlib.pyplot as pyplot
print ("Locale after importing pyplot:", locale.getlocale())
pyplot.figure()
print ("Locale after creating figure:", locale.getlocale())

Running the MWE on my computer:


§~/src> python3 mwe.py
Python version: 3.5.1 (default, Dec 09 2015, 07:29:36) [GCC]
Initial locale: ('nn_NO', 'UTF-8')
Changing locale to en_US
New locale: ('en_US', 'UTF-8')
Matplotlib version: 2.0.0b1+1771.g5dfe000
Locale after importing matplotlib: ('en_US', 'UTF-8')
Locale after importing pyplot: ('en_US', 'UTF-8')
Locale after creating figure: ('nn_NO', 'UTF-8')

§~/src> mv ~/.local/lib/python3.5/  ~/.local/lib/xpython3.5/ # Revert to system-supplied matplotlib
§~/src> python3 mwe.py
Python version: 3.5.1 (default, Dec 09 2015, 07:29:36) [GCC]
Initial locale: ('nn_NO', 'UTF-8')
Changing locale to en_US
New locale: ('en_US', 'UTF-8')
Matplotlib version: 1.5.1
Locale after importing matplotlib: ('en_US', 'UTF-8')
Locale after importing pyplot: ('en_US', 'UTF-8')
Locale after creating figure: ('en_US', 'UTF-8')

To help us understand and resolve your issue please check that you have provided
the information below.

  • Matplotlib version, Python version and Platform (Windows, OSX, Linux ...)
  • How did you install Matplotlib and Python (pip, anaconda, from source ...)
  • If possible please supply a Short, Self Contained, Correct, Example
    that demonstrates the issue i.e a small piece of code which reproduces the issue
    and can be run with out any other (or as few as possible) external dependencies.
  • If this is an image generation bug attach a screenshot demonstrating the issue.
  • If this is a regression (Used to work in an earlier version of Matplotlib), please
    note where it used to work.

Activity

  1. anntzer commented on Jul 9, 2016

    @anntzer
    Contributor

    A quick look indicates that the behavior is backend dependent. Non-interactive backends do not reset the locale. Qt5Agg resets it after importing figure; TkAgg resets it after importing pyplot.

  2. tacaswell commented on Jul 10, 2016

    @tacaswell
    Member

    If it is backend dependent that I suspect it is the default locale handling in the GUI frameworks.

  3. anntzer commented on Dec 2, 2016

    @anntzer
    Contributor

    I can confirm that this is due to locale handling by the GUI framework. tkinter appears to reset the locale when imported for the first time:

    $ python -c 'import locale; locale.setlocale(locale.LC_ALL, "fr_FR.utf8"); print(locale.getlocale()); import tkinter as tk; print(locale.getlocale())'
    ('fr_FR', 'UTF-8')
    ('en_US', 'UTF-8')
    

    Likewise Qt5 resets the locale when the QApplication is created.

    Thus the best thing that can be done is probably to add a note in the docs to that effect.

  4. added this to the milestone on Dec 2, 2016
  5. modified the milestones: , on May 3, 2017
  6. modified the milestones: v2.1.1, on Oct 21, 2017
  7. anntzer commented on Dec 29, 2018

    @anntzer
    Contributor

    I'm going to close this as far too specific to be worth an entry in the docs IMO.
    Still, thanks for the report.

  8. removed this from the future releases milestone on Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions