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.
Related
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']
)
I have a Excel-sheet, which contains variable and variable-labels. I would like to import this file into Stata. How can I do it?
Let's assume that your variable names are on the first row, and labels on the second row.
I would do:
import excel using file.xlsx, firstrow clear
foreach var of varlist _all {
local x = `var'[1]
label var `var' "`x'"
}
drop if [_n]==1
foreach var of varlist _all {
cap destring `var', replace
}
The first bit replaces the label of the variables with the variable label, which should be in the first row of your imported dataset. The second bit drops this row, and the destrings all variables for which this is possible without an error. The reason for this is that all variables will be imported as strings when you have the second row as variable labels.
This is the most typical case I encountered, but of course there may be other scenarios where you have to adopt different approaches.
I've built a Dash app to display a dataframe as a datatable. The code below:
df = DataFrame(Day=["Monday","Tuesday","Wednesday"],
Object=["Egg","Cat","Phone"],
Letters=["A","B","C"],
Food=["Milk","Egg","Cheese"])
colors=(background="#111111", text="#000000")
app = dash()
app.layout = html_div() do
dash_datatable(id="data_table",
columns = tuple([(name=x,id=x) for x in names(df)])[1],
data = df_to_datatable(df),
style_cell = (textAlign="center", fontSize=16,
backgroundColor="rgb(50,50,50)",
color="white"),
style_header = (backgroundColor="rgb(30, 30, 30)",),
style_table = (textAlign="center", minwidth="35%",
width="35%",maxwidth="35%",
marginLeft="auto",marginRight="auto"),
),
html_div(id="output_div")
end
results in the following table:
However, I'm having trouble defining the background color of the entire page. Based on this documentation, I've tried to define the background color when I call html_div():
app.layout = html_div(style=(backgroundColor=colors.background)) do
which results in the error:
Invalid argument `style` passed into Div.
Expected `object`.
Was supplied type `string`.
Value provided: "#111111"
I checked that I'm passing the tuple "colors", but it's reading the definition of the tuple argument as a string. How can I pass it the argument it's looking for?
Ultimately, I would like the white background to be blue.
You want:
html_div(style=(backgroundColor=colors.background,))
That trailing comma is important to differentiate between assignment and the creation of a named tuple.
I am attempting to write an algorithm that selects a specific reference standard (vector) as a function of temperature. The temperature values are stored in a structure ( procspectra(i).temperature ). My reference standards are stored in another structure ( standards.interp.zeroed.ClOxxx ) where xxx are numbers such as 200, 210, 220, etc. I have built the rounding construct and paste it below.
for i = 1:length(procspectra);
if mod(-procspectra(i).temperature,10) > mod(procspectra(i).temperature,10);
%if mod(-) > mod(+) round down, else round up
tempvector(i) = procspectra(i).temperature - mod(procspectra(i).temperature,10);
else
tempvector(i) = procspectra(i).temperature + mod(-procspectra(i).temperature,10);
end
clostd = strcat('standards.interp.zeroed.ClO',num2str(tempvector(i)));
end
This construct works well. Now, I have built a string which is identical to the name of the vector I want to invoke, but I'm uncertain how to actually call the vector given that this is encoded as a string. Ideally I want to do something within the for-loop like:
parameters(i).standards.ClOstandard = clostd
where I actually am assigning that parameter structure to be the same as the vector I have saved in the standards structure I have previously generated (and not just a string)
Could anyone help out?
Don't construct clostd like that (containing the full variable name), make it contain only the last field name instead:
clostd = ['ClO' num2str(tempvector(i))];
parameters(i).standards.ClOstandard = standards.interp.zeroed.(clostd);
This is the syntax of accessing a structure's field dynamically, using a string. So the following three are equivalent:
struc.Cl0123
struc.('Cl0123')
fieldn='Cl0123'; struc.(fieldn)
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.