How to add padding to title in `matplotlib.animation`? - python-3.x

I created an animation using python matplotlib.animation library and I'm quite happy with it. The output is
The only part I'm unhappy with is the title. As you can see it "jiggles" around. The update function in the animation looks like this
def update_curves(num):
# do stuff with data to get lines
title.set_text('September 5th, 1994 at 00:00h\n + {}y {}d'.format(int(time[num]/365),int(time[num]%365)))
return title, lines
Is there a way to stop the y and d from moving around?
I tried searching SO for info on how padding might work but I haven't really seen something that is compatible with matplotlib.animation. Using tabs also doesn't work since they don't get displayed properly...

You can do this using good old string formatting:
title.set_text('September 5th, 1994 at 00:00h\n + {:3}y {:5}d'.format(int(i), int(i*20)))
title.set_family("monospace")
{:3} formats the string so that it is exactly 3 characters long, and is left padded with spaces.
If you use the normal serif/sans-serif font, you will still have some jitter since each digit/letter is not equal in width, but set the font family to monospace and it works correctly.

Related

Minimise width of the multi-line text in Flutter

I have a variable-width container (screen width) and fixed left and right margins. Remaining area gives me a constraint for maximum text width, where I'm placing text of variable content (multiple languages). I want text to fit into that width without truncation, wrapping if needed. Text is relatively short - it may fit into single line, likely to fit into 2 lines and will fit into 3 lines for sure.
I want to avoid cases like (2) - where text wraps into long and a short line. It does not look nice. Instead I would like it to wrap more evenly, like in case (3).
For the demo purposes I've hardcoded hand-picked margins, but I want solution to work automatically for any text content and any container width.
Is it achievable using built-in Flutter widgets, or I need to implement something custom, similar to https://pub.dartlang.org/packages/auto_size_text?

Fastest way to get X/Y position of text or text-based shape in screenshot?

I'm trying to create a script for Linux that will detect where the text cursor is. This should be done in maximum 1 second. In order to implement this, the best solution seems to be to programmatically add some text via xdotool, take a screenshot via some other utility, try to figure out the position of that text, and then remove the text we've inserted using xdotool again.
I tried inserting a random string (like <-- CURSOR HERE). Using Tesseract 4 it takes about 20 seconds to find the position of the string, although it's very precise in terms of pixel coordinates. I was not able to use whitelisting (in version 4 of Tesseract) to narrow result to specific letters or digits only, which I assume would speed up processing.
I don't know what font the user will be using, but every font has dashes and slashes, so I could create some sort of shape (for instance, |/\|/\|/\|/\|), and use some library to detect that shape. What would be a good choice?
I don't care about what's on the rest of the screen: it could be more text, images, etc. I only need o know where my random string is (<-- CURSOR HERE, |/\|/\|/\|/\|, or can you think of anything else), and get its X/Y position in pixels.

Matplotlib using text instead of marker BUT can't be the best way to do it?

I would just like to make sure I am on the right track here as this seems to be pretty cumbersome for Matplotlib. I want to use a label as a marker on a plot and have it working to some degree. It uses mathtext BUT I wonder if there isn't another way to do it? Here is the code.
import matplotlib.pyplot as plt
x = []
y = []
symbol = "AAPL"
x = range(5)
y = [5,10,12,15,11]
plt.plot(x,y,lw=2.5,color='r',linestyle='solid',marker=r"$ {} $".format(symbol),markersize=25)
plt.show()
I am not 100% sure what you want, but below I have listed a couple of options that I am aware of for putting text into plots at specific locations (essentially what a marker is).
1) you can uses text/characters as markers through unicode. This is done by adding the unicode value within mathtext characters. E.g. take your example code; you can make the marker a unicode character by adding a 'u' before the string (unneeded in python 3) and then a '\u' and a 4 digit number. this will produce a unicode marker. Not all will work, as it depends on whether your system's font supports it. You can find a long list of them here: http://unicode-table.com/en/#latin-extended-a
plt.plot(x,y,lw=2.5,color='r',linestyle='solid',marker=u'$\u2609$',markersize=25)
\u2609 will produce a 'sun', i.e. a circle with a dot in its centre.
2) plt.text(...) using this function you can add text of your choice to the coordinate you specify.
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text
I believe the coordinate will correspond to the bottom left corner of the text box, but you can play around with it to make absolutely sure if you want. E.g.
plt.text(x,y,'string',fontsize=18)
However, this must be done on individual points and will not plot a line over the data; it does not work like 'plot' although you could always brute force a line over the top with a subsequent line plot. This method is more of a pain and hardly optimal but it will do the job and is quite flecible if you want a string for a marker.

Text/font spacing in TextLayer

I started to write my own watch face. I have problem with font spacing. I'm using custom .ttf font and my problem is that "1" and "9" digit does not have same width. When hour/minute changes from 1 to something different, whole text move. I don't know what to do with it.
I have 3 ideas to handle this problem :
- use png images of digits instead of font (more space needed, harder)
- take every digit as a single TextLayer (to display "00:00" I would need 4 TextLayers)
- some function build-in already - tried to find but without luck
I'm writing it in C.
Thanks,
Peter.
I downloaded some other watch faces and tried to play with them. Problem was with font, not my app. I used "FontForge" to modify width of "1" in font I want to use. Now everything is working fine without splitting etc..

How to display conjuncted letters [Bengali Language] using LWUIT in mobile?

I have been trying to develop a simple J2ME application using LWUIT in Bengali language. However, because of heavy usage of vowels as conjuncted letters in Bengali language, I am facing some problems with LWUIT.
For example let us say, “X” is a consonant letter and “#” works as a vowel in Bengali; now they are combined together when needed becoming a conjuncted format “X#”.
Using LWUIT, when I add such vowels and try to display them as the conjuncted format with a consonant in a real application, they are combined with their previous letter (which is in a consecutive order) as defined in the charset. Although interestingly, in the LWUIT designer display/preview, the characters appear correctly.
For details, kindly download this document here (http://dibbaa.com/lwuit/doc/lwuit.doc) and see the real-life examples.
I will appreciate if anybody can help me out on this. Just let me know how can I set LWUIT framework in such a way so that it doesn’t combine the letters as they defined in the charset by consecutive order while painting them.
I have used LWUIT version 1.3 and font “KarnaphuliP.ttf” for my application.
Thanks
I know nothing about Bangali, and I cannot find the font you mentioned. But I managed to get an alternative font for recreating the problem:"Bengali-Progoty.TTF" (which, unfortunately, is not a bit similar to yours). You can get the font here:Bengali-Progoty.TTF.
Those vowels are special, in that their width are zero ,and I bet their origin point is the right-top point, instead of left-top. This way, vowels can be drawn on top of other characters preceding them.
When lwuit designer generates bitmap font, it draws every character (What I mean is, unicode character) onto a big bitmap, calculates the width of current character, add that width to current offset, and draws the next character. As a vowel has a width of zero, it will be combined into the last non-vowel character preceding it.
To solve this problem, you can either switch to unicode font (Bangali has a place in unicode), or you can stick to the current font and do some customization work to the font generation process.
1 Create your own class overriding the EditorFont class in lwuit's editor.jar.
2 Override EditorFont#getBitmapFont() method, do your own drawing of every character. You can test if any character is a vowel, and if so, draw it with a preceding space.
3 Override the FontTask Ant task provided in lwuit's editor.jar.
4 Override the FontTask#addToResources() method, insert your own EditorFont instance instead of the original one.
5 Override the LWUITTask class, add an AddXXX method to support your overriden FontTask.
6 Build a resource using ant, and use your own version of LWUITTask and FontTask instead of the original version.
7 As vowels have become regular characters, they will take up the same space as other characters and cannot be drawn on top of other characters any more. You have to draw them on top of other characters manually. The com.sun.lwuit.CustomFont class may have to be overriden in order to draw these vowels correctly.
Given the complexity introduced, I highly recommend switching to unicode font. But as I have said, I know nothing about Bangali and cannot tell if it is adequate to use a unicode font. Maybe you have to do it the hard way after all.
Good luck.
I think it may be better for you to implement your own Virtual keyboard. There is a sample in the LWUIT developer guide and demo. I am sure if u invest time in that it will come in handy later on.

Resources