How to replace tooltip label in altair? - altair

I have a large dataset with coded column names. I made a dictionary with corresponding labels that explain the field, as such
codes = {
"Q1r1" : "first",
"Q1r2" : "second",
"Q1r3" : "third",
"Q1r4" : "etc...",
}
So now I would like to replace field names in tooltip with respective labels from codes.get(), but cannot figure out how to refer to outside dict from either .encode() or any .transforms.
Just to be clear: if the field referenced in below code is "Q1r1", I would like to have it shown in tooltip as "first" instead.
alt.Chart(df).mark_point().encode(
x='x:Q',
y='y:Q',
size='z:Q',
color=alt.Color('group:N'),
tooltip=['group:N', 'field:N']
)

How about mapping the group names first:
df['group_mapped'] = df['group'].map(codes)
alt.Chart(df).mark_point().encode(
x='x:Q',
y='y:Q',
size='z:Q',
color=alt.Color('group:N'),
tooltip=['group_mapped:N', 'field:N']
)

Related

Render multiple dicts in muliple tabs for a flask application

I want to render multiple dicts json to front.
All in the same interface.
But each one in a specific tab.
And the tabs have to be created dynamically based on the number of dicts.
A basic example
[{"name" = a, "age" = 29},{"name" = b, "age" = 19}, {"name" = c, "age" = 28}]
The first tab will be a table containing the first dict {"name" = a, "age" = 29}, the second tab a table containing {"name" = b, "age" = 19} and so one.
Knowing that I don't now the length of json in advance
The question is not very clear but from what I understood, you can do something like this:
{% for tab in my_tab_list %}
<tab> Name: {tab.name} Age: {tab.age} </tab>
{% endfor %}

Insert variable number of items with FORMAT ( )

I have a text with some strings that I want to replace with a variable. For example:
message = """I am a message for {user} and you have puchased the following items {items} with color {color}"""
There I want to replace {user}, {items} and {color} by a variable using the following code:
message = message_template.format(user='Ali', ID = ID1)
The problem is that in some cases I will have one item and in other cases more than 5 and I need to insert them independently. Also, color and item are part of a Dataframe.
Any idea about how could I insert a changing number of variables with .format( )?
Thanks
As for multiple items convert your list to a string using : ', '.join(items)
items = ['i1','i2','i3']
message = message_template.format(user='Ali', items = ', '.join(items), color='orange')

pythonstring value vs string in django orm

this is what I want
I made the parameter by str value.
because I have to get parameters by list variable.
but when I use str parameter in filter, wrong result is comming.
whole source is here.
In the first picture, you are providing a list of strings and in the second picture, you are providing string.
You can solve it by:
import json
fieldQueryString = json.loads(fieldQueryString)
this will convert this string into a list. So the output will change from
'["001", "002", "004", "005", "006"]' # this is a string
to
["001", "002", "004", "005", "006"] # this is a list
(notice the quotes before and after [ and ]).

how do I use variable name in bokeh HoverTool tooltips?

In Bokeh when using HoverTool, we end up using the "absolute name"
hover = HoverTool()
hover.tooltips = [
('name of salesperson','#name'),
('No. of Sales','#sale_num'),
('Sales Revenue in USD','#sale_rev')
]
p.add_tools(hover)
when the dataframe has the columns names as "name", sale_num" and "sale_rev".
Is there a way to use variable names rather than actual column names?
So, if I set
var_01 = "name"
var_02 ="sale_num"
var_03 = "sale_rev"
How do I use something like:
('name of salesperson','#var_01')
rather than the corresponding
('name of salesperson','#name')
Sure:
var_01 = "name"
var_02 = "sale_num"
var_03 = "sale_rev"
Then:
('name of salesperson','#' + var_01)
That will substitute things on the Python side of things, it will immediately generate:
('name of salesperson','#name')
because that's what's how standard Python string concatenation works, and then this is what gets sent to the browser.
If you are asking if there is some way to have this indirection cross over to the browser side (i.e. such that if you change the variable, the displayed content will update) the answer is No, because the browser knows nothing at all about your Python code or variables.

Stata - Add Prefix to Variable Values

I have many variables. For brevity, assume I have two: Gender and Meal. In Stata, I am using tabout, a package that allows one to produce .tex based on Stata results that can be opened as tables in LaTeX.
In order to create a customized output with a little spacing before the variable labels, I want to assign a prefix, \hspace{0.3cm}, to the beginning of all of the values (not labels) of each variable. How can I do this automatically with a loop instead of manually doing this?
Let's say I start out with this:
label def gen 0 "Male" 1 "Female", modify
label value Gender gen
label def me 0 "Lunch" 1 "Dinner", modify
label value Meal me
I want to have a loop that will automatically add the prefix to the individual values of Gender and Meal. The end result would be the same as if I had originally done:
label def gen 0 "\hspace{0.3cm}Male" 1 "\hspace{0.3cm}Female", modify
label value Gender gen
label def me 0 "\hspace{0.3cm}Lunch" 1 "\hspace{0.3cm}Dinner", modify
label value Meal me
Note that code (from http://www.jwe.cc/2012/03/stata-latex-tables-estout/) to do a similar thing for variable labels (and NOT values) is as follows:
foreach v of varlist * {
label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
}
Here is some code that produces the strings you want. I leave to you defining the new value labels and assigning to the variables. Let us know if it's useful.
clear all
set more off
*----- example -----
label def gen 0 "Male" 1 "Female", modify
*label value Gender gen
label def meal 0 "Lunch" 1 "Dinner", modify
*label value Meal me
*----- what you want -----
label dir
local rnames `=r(names)'
foreach labname of local rnames {
quietly label list `labname'
local myname
forvalues i = 0/`r(max)' {
local name : label `labname' `i', strict
local newname \hspace{0.3cm}`name'
local myname `myname' `newname'
}
display "`myname'"
}
You can make it a bit shorter, but it's all very "explicit".
help label and help extended_fcn are a must-read.
(I still insist that a solution within tabout is maybe possible; but I can't be sure.)
Edit
The following is more general, has better form and is a complete example. Extended macro functions are still the basis for the code.
clear all
set more off
*----- example database -----
sysuse voter
*----- what you want -----
foreach var of varlist _all {
local cnewname
quietly labellist `var'
if "`r(lblname)'" != "" {
*disp "`var'"
forvalues i = 1/`r(`r(lblname)'_k)' {
local val : word `i' of `r(values)'
local labval : word `i' of `r(labels)'
local newname `val' "\hspace{0.3cm}`labval'"
local cnewname `cnewname' `newname'
} // forvalues
label define newlbl`var' `cnewname'
label value `var' newlbl`var'
} // if
} // foreach
labellist
I define new value labels and re-associate with corresponding variables. You can try replacing or whatever fits your needs.
Stata doesn't understand TeX or LaTeX, at least not like this.
You could just prefix with space(s), but often Stata would just ignore them any way.
A bizarre trick I've used occasionally is to use char(160) as a pad which looks like a space but won't be trimmed.
length(trim("`=char(160)'"))
is reported as 1, i.e. char(160) is not trimmed. To check that char(160) is invisible on your machine,
di char(160)
But how this works surely depends on your TeX/LaTeX code and how it treats that character.

Resources