Both latex and parameter values in python strings - python

I want to add a title to my plots (in matplotlib) which would display both parameters and their values, something like
f"$\alpha$ = {present value of alpha}, $\beta$ = {present value of beta}"
f-strings won't display latex but will insert the present parameter values into the placeholders; r-strings will do the former but not the latter.
Any way around this?

Related

Map string values in Dataframe to integers using Python

So I have a Dataset which has a column containing name of colors(red, blue, green) and I want to convert these string values into int/float to fit into classifier. I was planning on using Python dictionary with keys as name of color and values as numbers.
This was my code:
color_dict = {'red':1, 'blue':2, 'green':3}
for i in train['column_name']:
train['column_name'][i] = color_dict[i]
print(train['column_name'])
Sadly, this did not work.
What should I do differently to make it work?
The answer is in the question :)
train["column_name"] = train["column_name"].map(color_dict)
See the docs for map.
The reason your solution didn't work is a bit tricky. When you access a value like you did (using chained brackets), you're working on a copy of the DataFrame object. Instead, use train.loc[i, "column_name"] = color_dict[i] to set the a single value in a column. See here for more details.

use custom functions in transform_calculate in altair

i have a bar chart, that i want to add approximation of each bar value, above each itself.
that value is a big number, i have a function that formats number e.g.: 10,000$ to 10k$. how can i apply that.
base = alt.Chart(target_df_hahu).mark_bar().encode(
alt.X('monthdate(date):O'),
alt.Y('value'),
color = 'variable'
)
i have try blow code .
text = base.mark_text().encode(
text = 'value:Q'
).transform_calculate(
value=custom_function(datum.value)
)
base+text
Calculate transforms are evaluated in Javascript by the Vega renderer, and thus cannot contain custom functionality defined in Python. Calculate transforms must be strings containing a well-defined subset of javascript syntax, making use of any of the functionality listed at https://vega.github.io/vega/docs/expressions/

Unreadable values in heatmap

I have a dataframe df containing numeric values 1432,22390,43223 and so on...
When I try to plot it using a heatmap like this sns.heatmap(df[cols].transpose(), annot=True)
I'm getting these unreadble values 2.2e+04, 1.7e+03 etc.
The thing is that in another notebook I'm using the same code and it works perfectly.
So what is the problem?
When you set annot=True, the default param fmt='.2g' is being applied (i.e, it is going to round each number to 2 significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude).
To have the general format, you simply change fmt='.2g' to fmt='g'.
Here you can find more information about formats

Loop through possible values a class type

I'm learning how to use Python and Basemap and would like to create a loop that produces a map of each projection type.
The projection types are: cea, mbtfpq, aeqd, sinu, poly, etc. So I just want a loop that does Basemap(width=x, height=y, projection=[projection type], ...) but can't figure out how to return the actual types of possible projections.
So far I've tried things like
proj = Basemap()
print(dir(proj))
and
proj = Basemap().projection
print(dir(proj))
but neither returns the types of projections it could be. I tried
for value in Basemap().projection:
print (value)
But it just returned
c
y
l
and that's it.
Closest I've gotten is
for value in Basemap().__dict__.items():
print (value)
but that returns a lot of info, seemingly the default values, but one of them is cyl, which is the default projection. I'm getting close but can't see how to iterate through each projection.
(My semantics are incorrect, so please correct me if I'm wrong!)
Edit: I'd like to learn how to do this without "cheating", i.e. since I know the types of projections possible, load those into an array and loop through the array. I'm trying to learn how to do it if I didn't know the possible values.
There's no need to cheat; looking at the source, you have a supported_projections list that contains all supported projections. You can just use that.

Using two different text sizes in one legend in Python

I am using matplotlib
I have a legend on a graph in Python which is:
plt.text(sigma1+5,5,str("$\sigma$("+"%.2f"%(sigma1) + ",0)"),color='red')
I'd like the symbol sigma to be larger than the rest of the text. Is this possible? Or do I have to create two separate legends?
Unfortunately, a matplotlib.text.Text instance uses a single style (font, size, etc.) for the whole string. So yes, I'm pretty sure you're going to need to create two of them.
If you don't know how to set the font size, see the docs for matplotlib.pyplot.text: you can either pass an optional fontdict argument that specifies font properties, or you can pass extra keyword arguments like size or fontproperties that get passed on to the Text constructor.

Categories