1
votes

The challenge:

How can you prevent Matplotlib from using commas in figure arguments like matplotlib.pyplot.figure(figsize=(3,75,3,5)) without changing your locale settings (and still use commas as decimal separators for the rest of your system)?


The details:

From the questions and answers in the posts What is the best data format and setup for a time series in a Python Visualization in Power BI? and What setup do you need for Python Visualizations in Power BI? Any particular matplotlib package versions or system settings?, it seems that many european Power BI users will face a few issues when it comes to Python Visualizations in Power BI. This is because regional settings and using comma as a decimal separator raises an issue in matplotlib.

In order to avoid the errors matplotlib.pyplot.figure(figsize=(3,75,3,52)) and TypeError: from_bounds() takes 4 positional arguments but 6 were given when you're setting up a Python Visualization, it seems that you would have to:

  1. Use non-european regional settings for you entire system, or

  2. tell matplotlib to ignore regional settings when setting up a figure.

At least to me, It would be unthinkable to use another regional setting and decimal separator than ",". As far as I know, that would mess up the numerous other reports what we've set up in Power BI.

So I guess we're stuck with option 2.

In the post Matplotlib : Comma separated number format for axis, one of the answers shows how this can be done using:

import locale
locale.setlocale(locale.LC_ALL, "deu_deu")
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True

But it doesn't seem to work in Power BI. I've tried with this simple dataset:

A,B
1,11
2,19
3,18
4,19
5,18
6,12
7,12
8,19
9,13
10,19

And this script:

#Locale settings
import locale
locale.setlocale(locale.LC_ALL, "deu_deu")
import matplotlib as plt
plt.rcParams['axes.formatter.use_locale'] = True

# Plot
import matplotlib.pyplot as plt
plt.plot(dataset['A'], dataset['B'])
plt.show()

But still no success:

enter image description here

I've also tried other arguments than "deu_deu" and I've tried setting plt.rcParams['axes.formatter.use_locale'] = True to False. And of course, I've tried different settings in Power BI like setting Locale for Import under Options | Regional Settings to English (United States):

enter image description here

3
Matplotlib is written in python. The python language uses floating point numbers and the decimal separator for a float object in python is a dot. I'm not aware of any other programming languages that would change their syntax depending on some (locale) setting but those might exist. In any case, because power bi simply executes python as it seems, there is no way it would allow to use anything else but valid python syntax. Concerning string representations on screen, the usual locale formatting is of course available and can be used within python and matplotlib.ImportanceOfBeingErnest
In that sense it seems like a bug in power bi. Where do those numbers 3,75,3,52 actually come from? Do you write them somewhere or are they generated by power bi?ImportanceOfBeingErnest
they are generated by matplotlib when running a scripy in power bi. on my phone right now, but I'll provide more details as soon as I canvestland
No, matplotlib does not generate wrong figure sizes.ImportanceOfBeingErnest
In that case, this is becoming really interesting. The numbers you can see in the screenshot are in fact produced by the script further down in the same picture. Which is the same as in the provided snippet. I'm away on travel right now, but I'm looking forward to providing more details on friday.vestland

3 Answers

1
votes

I think this is definitely a bug that Power BI team should work on to fix it.

Similar reports by Power BI users can be found here:

https://community.powerbi.com/t5/Issues/Bug-in-python-visual/idc-p/488205

For the time being, you'll have to add the line matplotlib.pyplot.figure(figsize=(3.75,3.52)) using . as the decimal point to avoid Power BI generating a bugged line for you. (which also applies to any other functions that you may come across where Power BI may be generating extra codes to display the Python visuals)

0
votes

Adding the line matplotlib.pyplot.figure(figsize=(3.75,3.52)) as suggested by Foxan Ng does not seem to work on my end. The only thing that does seem to work is changing the decimal separator to . in the Windows settings under Control Panel | Region and Language | Formats | Additional Settings | Decimal Separator (I'm still on Windows 7) . Now it works:

enter image description here

But now of course, I'm running the risk of messing up everything else in other Power BI reports. So I'm really hoping that the Power BI team gets on this or that others are able to provide even better answers.

0
votes

Workaround by changing the decimal separator in the Control Panel worked out for me. Don't forget to close and re-open PBI after having applied it to actually apply the change.