我正在尝试使用 Seaborn 将一维数据框呈现为水平条形图。我想使用冷暖调色板对条形进行着色以反映它们的大小和方向。
换句话说,我希望生成类似此处显示的第二个示例的内容(来自Seaborn 站点),但我想将其水平定向:
我已经成功地将图表转向侧面,但我似乎无法使调色板也沿水平轴应用。我的代码:
import pandas as pd, seaborn as sns
sns.set()
df = pd.DataFrame([7,-5,-2,1.5,-3],
index=['question 1','question 2','question 3','question 4','question 5'],
columns=['t'])
sns.barplot(data= df,
x= 't',
y= df.index,
palette= 'coolwarm')
Run Code Online (Sandbox Code Playgroud)
输出:
我希望当您从左到右(而不是从上到下)移动时它从蓝色变为红色。
在命令行 Python 会话中,您可以使用下划线获取上一个表达式的输出:
> 5 * 6
30
> _ + 2
32
Run Code Online (Sandbox Code Playgroud)
朱莉娅有类似的东西吗?
对于标量,\(求解线性系统)运算符等效于除法运算符/。性能相似吗?
我问是因为目前我的代码有一行
x = (1 / alpha) * averylongfunctionname(input1, input2, input3)
Run Code Online (Sandbox Code Playgroud)
从视觉上看,除法alpha发生在“左侧”很重要,所以我正在考虑将其替换为
x = alpha \ averylongfunctionname(input1, input2, input3)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,从风格和性能的角度来看,最佳实践是什么?
以下是一些令人困惑的基准测试结果:
julia> using BenchmarkTools
[ Info: Precompiling BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]
julia> @btime x[1]\sum(x) setup=(x=rand(100))
15.014 ns (0 allocations: 0 bytes)
56.23358979466163
julia> @btime (1/x[1]) * sum(x) setup=(x=rand(100))
13.312 ns (0 allocations: 0 bytes)
257.4552413802698
julia> @btime sum(x)/x[1] setup=(x=rand(100))
14.929 ns (0 allocations: 0 bytes)
46.25209548841374
Run Code Online (Sandbox Code Playgroud)
它们都差不多,但令我惊讶的是该(1 / x) * foo 方法具有最佳性能。
test = [1, 1, 1, 1]
ifelse(isassigned(test, 5), test[5], "nope")
Run Code Online (Sandbox Code Playgroud)
对我来说,这应该产生字符串“nope”,但我得到
BoundsError: attempt to access 4-element Array{Int64,1} at index [5]
Run Code Online (Sandbox Code Playgroud)
这是错误还是预期的功能?
现在,我正在使用
if isassigned(test, 5) test[5] else "nope" end
Run Code Online (Sandbox Code Playgroud)
但这在列表理解中不是很清晰。
在 Rust 中,我发现我们可以使用.last()函数来访问向量的最后一个元素。但是,当我尝试将其插入到打印输出中时,如下所示
fn main() {
let mut v: Vec<f64> = Vec::new();
v.push(1.0); v.push(1.9); v.push(1.2);
println!("Last element is {}", v.last()); // (*)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
error[E0277]: `Option<&f64>` doesn't implement `std::fmt::Display`
Run Code Online (Sandbox Code Playgroud)
完整的错误文本提示我可以使用它{:?}。如果我更改(*)为 read println!("Last element is {:?}", v.last()); // (*),则程序会打印
Last element is Some(1.2)
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到Last element is 1.2?
如何将值提取1.2为f64?
在 C# 中,您可以向类型附加问号以指示它可以保存类型的值或返回 null:
double? MySqrt(double a)
{
return (a > 0.0) ? Math.Sqrt(a) : null;
}
Console.WriteLine(MySqrt(4.5)); // 2.1213203435596424
Console.WriteLine(MySqrt(-4.5)); // a blank line
Run Code Online (Sandbox Code Playgroud)
现在,如果我想将此函数的结果作为变量赋值的条件,我可以使用??运算符,例如
double b = MySqrt(-2.0) ?? 0.0;
Console.WriteLine(b); // 0
Run Code Online (Sandbox Code Playgroud)
但是,如果不重新定义MySqrt,我如何根据是否MySqrt返回来控制更大的编程流程(而不仅仅是单个变量赋值) null?
例如,以下内容会产生编译器错误:
void DrawSquareWithArea(double area)
{
double sideLength = MySqrt(area);
if (sideLength != null)
{
// Draw a square on the screen
DrawSquare(sideLength);
}
else
{
// Display some visual feedback indicating that input was …Run Code Online (Sandbox Code Playgroud)