I want to use bold text for the labels of my python plot. I tried this:
plt.xlabel("$\mathrm{\\mathbf{\delta\\langle r^2\\rangle^{226,A}[fm^2]}}$",fontsize=50,fontweight='bold')
But the output looks weird (see the attached picture). The letters get bold, but the numbers and the symbols stay the same. How can I get everything in bold? Thank you!
Using weight instead of fontweight.
Update:
I found that using \boldsymbol will work with the Greek letter.
\boldsymbol{\mathbf{text}}
Related
Unfortunately due to a problem with windows I cannot render any of my matplotlib text with LaTeX. So basically I need away already in matplotlib's text handling to place a line underneath a letter.
So far the closest I have is ax.text(x,y,z, r'$\mathbf{\underbar r}$'), but this just produces _r. So if there is a way to get this bar under the 'r' that would be amazing. Thanks in advance!
P.S. I have already tried ax.text(x,y,z, r'$\underline{\mathbf{r}}$') but this doesn't seem to work, :,(
Edit
Just realized that you can 'cheat' matplotlib :D by placing text in the exact same position with just an \underbar, which makes it appear under the letter. i.e. ax.text(x,y,z, r'$\mathbf{r}$') followed by ax.text(x,y,z, r'$\mathbf{\underbar}$')
But a quicker way would still be appreciated, thanks!
You could use some negative spacing between the underbar and your character. In this case it seems that tripling the \! works well:
ax.text(2,7, r'$\mathbf{\underbar \!\!\! r }$')
I have done some research on formatting string but it does not want to work for me. I have this
for i in range(0,10):
stat = arr[i]
highscoreText = GameFont.render('{0:12}{1:>0}'.format(stat["Name"],stat["Score"]),2,(255,255,255))
Screen.blit(highscoreText,[50,50 + (i*30)])
Output: http://prntscr.com/b9abfw
The name works but I can't seem to make the Score align to the right.
The string formatting works as expected. Try to print formatted sting in console. The problem with the font you use. See, ll in hello take the same span as k below.
To solve this you have to render names and scores separately and then blit them at appropriate positions.
Or you can change the font you use to a monospace, like Courier or Dejavu mono
String formatting assumes that you are using a monospace font. Since you have decided to use a proportional font you will need to draw as separate blocks and use the graphics routines to align each block to the right.
I have seen this issue pop up here and there but have yet to find a suitable answer.
When making a plot in matplotlib, the only way to insert symbols and math functions (like fractions, exponents, etc...) is to use TeX formatting. However, by default TeX formatting uses a different font AND italicizes the text. So for example, if I wanted an axis label to say the following:
photons/cm^2/s/Angstrom
I have to do the following:
ax1.set_ylabel(r'Photons/$cm^2$/s/$\AA$')
This produces a very ugly label that uses 2 different fonts and has bits and pieces italicized.
How do I permanently change the font of TeX (Not the other way around) so that it matches the default font used by matplotlib?
I have seen other solutions that tell the user to manually make all text the same in a plot by using \mathrm{} for example but this is ridiculously tedious. I have also seen solutions which change the default font of matplotlib to match TeX which seem utterly backwards to me.
It turns out the solution was rather simple and a colleague of mine had the solution.
If I were to use this line of code to create a title:
fig.suptitle(r'$H_2$ Emission from GJ832')
The result would be "H2 Emission from GJ832" which is an illustration of the problem I was having. However, it turns out anything inside of the $$ is converted to math type and thus the italics assigned.
If we change that line of code to the following:
fig.suptitle(r'H$_2$ Emission from GJ832')
Then the result is "H2 Emission from GJ832" without the italics. So this is an example of where we can constrain the math type to include only the math parts of the text, namely creating the subscript of 2.
However, if I were to change the code to the following:
fig.suptitle(r'H$_{two}$ Emission from GJ832')
the result would be "Htwo Emission from GJ832" which introduces the italics again. In this case, and for any case where you must have text (or are creating unit symbols) inside the dollar signs, you can easily remove the italics the following way:
fig.suptitle(r'H$_{\rm two}$ Emission from GJ832')
or in the case of creating a symbol:
ax2.set_xlabel(r'Wavelength ($\rm \AA$)')
The former results in "Htwo Emission from GJ832"
and the latter in "Wavelength (A)"
where A is the Angstrom symbol.
Both of these produce the desired result with nothing italicized by calling \rm before the text or symbol in the dollar signs. The result is nothing italicized INCLUDING the Angstrom symbol created by \AA.
While this doesn't change the default of the TeX formatting, it IS a simple solution to the problem and doesn't require any new packages. Thank you Roland Smith for the suggestions anyway. I hope this helps others who have been struggling with the same issue.
For typesetting units, use the siunitx package (with mode=text) rather than math mode.
Update: The above is only valid when you have defined text.usetex : True in your rc settings.
From the matplotlib docs:
Note that you do not need to have TeX installed, since matplotlib ships its own TeX expression parser, layout engine and fonts.
And:
Regular text and mathtext can be interleaved within the same string. Mathtext can use the Computer Modern fonts (from (La)TeX), STIX fonts (with are designed to blend well with Times) or a Unicode font that you provide. The mathtext font can be selected with the customization variable mathtext.fontset
Reading this, it sounds that setting mathtext.fontset and the regular font that matplotlib uses the same would solve the problem if you don't use TeX.
I'm trying to add some axis-labels to a graph which contains the Greek letter 'rho'. To do this I want to use the LaTeX capability of Matplotlib but it seems to have a problem with the \rho symbol.
Here is a minimal example:
import matplotlib.pyplot as plt
from matplotlib import rc,rcParams
rc('text',usetex=True)
rcParams.update({'font.size': 16})
plt.plot([0,1,2,3,4],[0,1,4,9,16])
plt.xlabel('\rho A_i') # works if \rho is replaced with, for example, \sigma
plt.ylabel('Something else')
plt.show()
Upon running the first time I get a bunch of LaTeX errors and a blank figure window, running again shows the graph but the xlabel reads 'ho Ai ' where the i is subscript as expected.
The weird thing is if I replace \rho with something else, say, \sigma it shows up correctly. Can anybody tell me why it is not happy with my code example and how to fix it?
Thanks.
P.s. I tried putting the expression in $..$ but that changed nothing.
I think you are supposed to use raw strings, and use the $ signs as well. Try:
plt.xlabel(r'$\rho A_i$')
Be careful when you're using \n , \r and so on in a string. Those are commands for e.g. entering a new line etc.
https://docs.python.org/2/library/re.html
To make sure you don't use these regular expression operators put \\rho instead of \rho.
Is there a way to specify the color a text is printed within Idle for Python 3.2?
I'm looking for something like:
print("foo", "#fafafa")
print("bar", "#4f4f4f")
http://docs.python.org/library/idle.html#syntax-colors
To change the color scheme, edit the [Colors] section in config.txt.
Edit: As you've edited your question, here is an edited answer. See
http://www.daimi.au.dk/~mailund/scripting2005/lecture-notes/process-management.html
for how to use terminal escape sequences in Python to change the color of things.
Will they work in IDLE? I don't know. But they will work in most terminals.