Cyrillic text on python graphs, is that an OS issue?
- Anmelden oder Registrieren um Kommentare zu schreiben
Hello dear community!
I want to have Cyrillic text on python graphs. I know how to that with python 2 on Ubuntu and with python 3 in another OS which I do not want to even say it's name (the company I work in, use that OS, so I must work with it), but I can not do it with python 3 on Trisquel, even if I use same commands. I wonder if it is an OS issue?
Thanks for helps
Farhad
> I want to have Cyrillic text on python graphs.
> but I can not do it with python 3 on Trisquel, even if I use same
> commands.
What did you try and what was the result?
import matplotlib.pyplot as plt
# calculating x and y
plt.figure(1)
plt.clf()
plt.plot(x,y)
plt.text(0.5,0.5,'Привет!')
____________________________________________________
I get rectangles instead of letters.
You need to change matplotlib's font to one that includes the desired characters. For example like this:
------
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc("font", family="DejaVu Sans")
plt.text(0.5,0.5,'Привет!')
plt.show()
------
You can make these changes permanent by copying the file from /usr/share/matplotlib/matplotlib.conf/matplotlibrc.template to ~/.config/matplotlib/matplotlibrc, editing the line for the backend to choose your desired backend, and editing the line for font.family. So:
------
backend: TkAgg
font.family: DejaVu Sans
------
You can of course choose a different backend or font.
> I get rectangles instead of letters.
> import matplotlib.pyplot as plt
>
> # calculating x and y
>
> plt.figure(1)
> plt.clf()
> plt.plot(x,y)
> plt.text(0.5,0.5,'Привет!')
I don't know how to use matplotlib. I was able to import it after
installing `python3-matplotlib`, but in order to replicate your issue I
would need complete, working code. I don't know what types `x` and `y`
are supposed to be, but I guesed list and set them each to [0, 1, 2].
The remaining commands print the return types to the console but do not
produce a plot.
That makes it sound like you don't have any Cyrillic fonts installed (or
that your program is not finding them). Take a look at the output of
$ apt search fonts- cyrillic
and see if any of those packages work. I can't tell you for certain
which one(s) will work, since I can't replicate your issue, but I think
one of them might fix it.
You guessed correctly as to what x and y need to be. In order to produce a plot, there's a missing call to plt.show() at the end. A short working example is
------
import matplotlib.pyplot as plt
plt.text(0.5, 0.5, "Привет!")
plt.show()
------
The problem actually isn't missing fonts. Matplotlib just defaults to Bitstream Vera Sans if you have it installed, and that one doesn't seem to support Cyrillic.
Thanks a lot.
I did what you suggested and it worked well.
By the way, since I began to use Spyder with python 3, I have not needed to use
plt.show()
I get plots anyway.
- Anmelden oder Registrieren um Kommentare zu schreiben