考虑以下 python3 PyQt 代码来显示带有工具栏的交互式 matplotlib 图形
import sys, sip
import numpy as np
from PyQt5 import QtGui, QtWidgets
from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
app = QApplication(sys.argv)
top = QWidget()
fig = plt.figure()
ax = fig.gca()
x = np.linspace(0,5,100)
ax.plot(x,np.sin(x))
canvas = FigureCanvas(fig)
toolbar = NavigationToolbar(canvas, top)
def pick(event):
if (event.xdata is None) or (event.ydata is None): return
ax.plot([0,event.xdata],[0,event.ydata])
canvas.draw()
canvas.mpl_connect('button_press_event', pick)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar) …
Run Code Online (Sandbox Code Playgroud) I have a Qml component that is reasonably large so that I want to make it a reusable component but it is too small/non-generic such that I want to avoid creating its own .qml file. It seems like Components are the right method to define reusable objects in the same file, but when I do that I don't know how to access and change properties of the contained objects.
More specifically, imagine having an in-file definition like this
Component { …
Run Code Online (Sandbox Code Playgroud) 我试图std::enable_if
在C++ 11中更好地理解并且一直在尝试编写一个最小的例子:一个A
具有成员函数的类,void foo()
它具有基于T
类模板中的类型的不同实现.
下面的代码给出了期望的结果,但我还没有完全理解它.为什么版本V2
有效,但不是V1
?为什么U
需要"冗余"类型?
#include <iostream>
#include <type_traits>
template <typename T>
class A {
public:
A(T x) : a_(x) {}
// Enable this function if T == int
/* V1 */ // template < typename std::enable_if<std::is_same<T,int>::value,int>::type = 0>
/* V2 */ template <typename U=T, typename std::enable_if<std::is_same<U,int>::value,int>::type = 0>
void foo() { std::cout << "\nINT: " << a_ << "\n"; }
// Enable this function if T …
Run Code Online (Sandbox Code Playgroud) 我拥有的:
我想变成模块的本地 Python3 文件 test_module
test_module
包含一个空的__init__.py
、一个setup.py
文件(见下文)和包含多个源文件的子目录的文件夹
我想要的是:
在test_module
当地不断努力和改进
有一种简单的方法可以test_module
在我自己的虚拟环境中本地安装及其所有依赖项(使用创建python3 -m venv my_environment
)
运行通过 使用模块的文件python myexample.py
,而不必在每次进入或退出时调整我的本地 PYTHONPATH 变量my_environment
通过 git 与其他人共享我的 python 代码,并允许他们使用相同的过程在他们的机器上本地安装他们的代码(尽可能简单)
学习如何创建自己的模块的最佳实践
我现在是怎么做的:
pip freeze > requirements.txt
并pip install -r requirements.txt
用于安装依赖项
添加export PYTHONPATH="${PYTHONPATH}:."
到my_environment/bin/activate
, 在搜索路径中拥有我自己的模块(如下所示:How do you set your pythonpath in an already-created virtualenv?)
我想知道是否有基于 的“更清洁”的解决方案setup.py
,可能涉及pip install ./test_module
处理 2.-3 的类似或类似内容。自动地。
我当前的setup.py
文件如下所示
from …
Run Code Online (Sandbox Code Playgroud) 两个问题合二为一:鉴于在 Julia 中绘制的一条线,我如何才能
作为下面代码中的一个具体示例,我如何 1. 删除以前的回归线或 2. 将它们的不透明度更改为 0.1?
using Plots; gr()
f = x->.3x+.2
g = x->f(x)+.2*randn()
x = rand(2)
y = g.(x)
plt = scatter(x,y,c=:orange)
plot!(0:.1:1, f, ylim=(0,1), c=:green, alpha=.3, linewidth=10)
anim = Animation()
for i=1:200
r = rand()
x_new, y_new = r, g(r)
push!(plt, x_new, y_new)
push!(x, x_new)
push!(y, y_new)
A = hcat(fill(1., size(x)), x)
coefs = A\y
plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
# 1. delete previous line
# 2. set alpha …
Run Code Online (Sandbox Code Playgroud) I'd like to have Jest testing for some JavaScript code that I'm using in the frontend of my web app. From what I understand, I need to export and import functions from my module so I can test them in Jest, and Jest (only) supports CommonJS module syntax out of the box. As a result, my code looks as follows:
<!-- index.html -->
<!DOCTYPE html>
<html><body><div>Hello World</div>
<script src="./utils.js">
console.log('foo: ' + foo());
</script>
</body></html>
Run Code Online (Sandbox Code Playgroud)
// utils.js
function foo() {return …
Run Code Online (Sandbox Code Playgroud) 我想构建一个数字包,它还具有可视化的可选支持。为简单起见,假设各自的依赖项用于NumPackage
繁重的工作和VizPackage
可选的可视化。
在 Julia 中,我如何构建一个具有所需依赖项但仅作为可选依赖项的模块NumPackage
,例如对于VizPackage
那些想要运行示例模拟并将其可视化的用户?
我看到了Requires.jl包,但不确定它是否是适合我想要做的事情的工具。
有没有办法确定 julia 脚本myprog.jl
是通过命令行调用的julia myprog.jl
还是通过 REPL调用的include("myprog.jl")
?
背景:我正在使用ArgParse.jl包,因为我无法从 REPL 传递命令行参数,所以我只想ARGS = "argA --optB 1 --flagC"
在调用之前设置一个变量include("myprog.jl")
,以获得与julia myprog.jl argA --optB 1 --flagC
命令行相同的结果。为此,我需要知道程序是从命令行调用还是从 REPL 调用,这样我就可以编写类似
if called_from_repl
parse_args(split(ARGS),s)
else
parse_args(s)
end
Run Code Online (Sandbox Code Playgroud) 我正在挖掘源代码,Base.IteratorsMD
我确信Base.nextind
for CartesianIndex
(in julia/base/multidimensional.jl
,请参见此处https://github.com/JuliaLang/julia/blob/55e36cc308b66d3472990a06b2797f9f9154ea0a/base/multidimensional.jl#L142-L150 ) 的实现效率极低:
function Base.nextind(a::AbstractArray{<:Any,N}, i::CartesianIndex{N}) where {N}
iter = CartesianIndices(axes(a))
return CartesianIndex(inc(i.I, first(iter).I, last(iter).I))
end
Run Code Online (Sandbox Code Playgroud)
我认为CartesianIndices(...)
在每次调用中创建一个新实例必须非常昂贵,因为在我的机器上CartesianIndices(...)
通过 REPL调用似乎创建了所有索引。因此,我为替代实现编写了以下基准脚本mynextind
:
using Base.IteratorsMD
function mynextind(a::AbstractArray{<:Any,N}, i::CartesianIndex{N}) where {N}
dims = (x.stop for x in axes(a))
return CartesianIndex(Base.IteratorsMD.inc(i.I, CartesianIndex(ones(Int64, length(dims))...).I, CartesianIndex(dims...).I))
end
function f(func, M)
c = CartesianIndex(1,1)
while true
(c = func(M, c)) == CartesianIndex(size(M)...) && break
end
end
A = rand(100,100)
@btime f(Base.nextind, A)
@btime f(mynextind, A) …
Run Code Online (Sandbox Code Playgroud) 我view
对朱莉娅对稀疏矩阵的行为感到困惑不解:
using LinearAlgebra, SparseArrays, BenchmarkTools
v = SparseVector(1000, [212,554,873], [.3, .4, .3]);
A = sparse(diagm(rand(1000))); # same effect observed for non-diag matrix
B = view(A, :, :);
julia> @btime A*v;
1.536 ?s (4 allocations: 23.84 KiB)
julia> @btime B*v;
11.557 ms (5 allocations: 288 bytes)
Run Code Online (Sandbox Code Playgroud)
B*v
似乎占用的内存少得多,但比慢8000倍A*v
。这是怎么回事,是什么导致这些性能差异?
我试图将一些tikz图组成一个更大的概览图,但是我很难将子图像相对于其父对象以及彼此对齐。
使用scope
和从其他tikz图形中以矩形形状裁剪出子图像clip
:
\documentclass[crop,tikz]{standalone}
\usepackage{relsize, xcolor, tikz}
\begin{document}
\begin{tikzpicture}[x=0.04cm,y=-0.04cm]
\begin{scope}[xshift=0, yshift=0, local bounding box=scopeAouter]
\fill [black!40, rounded corners=5] (0, 0) rectangle ++(60, 70); % bounding rectangle
\begin{scope}[xshift=0, yshift=0, scale=.5, local bounding box=scopeAinner]
\clip [rounded corners=3] (-30, -5) rectangle ++(100, 50); % cannot change this line
% dummy pattern
\fill [red!60] (-100,-100) rectangle (100, 100);
\fill [blue!60] (-100, -100) -- (100, -100) -- (100, 100) -- cycle;
\end{scope}
\end{scope}
\begin{scope}[xshift=80, yshift=0, local bounding box=scopeBouter]
\fill [black!40, rounded corners=5] …
Run Code Online (Sandbox Code Playgroud) 我想push!()
在 for 循环中使用来继续两条单独的绘图线。考虑以下示例:
using Plots; gr()
f = x->x*x
g = x->f(x)+.2*randn()
p = plot(-1:.1:0, f, ylim=(-1,2), c=:blue)
s = scatter!(-1:.1:0, g, c=:red)
anim = Animation()
for i=1:10
x = i/10
push!(p, x, f(x))
push!(s, x, g(x)) # without this, f gets continued as expected
frame(anim)
end
gif(anim, "test.gif", fps=2)
Run Code Online (Sandbox Code Playgroud)
为什么push!(p, ...)
和push!(s,...)
两者都继续蓝线?如何将分散的数据附加到s
?
我知道此链接的第二个图通过同时绘制和推动两条线获得了类似的结果,但该解决方案并不总是实用的,尤其是在更复杂的图中。
julia ×6
benchmarking ×2
performance ×2
plot ×2
python ×2
qml ×2
animation ×1
append ×1
c++ ×1
c++11 ×1
commonjs ×1
components ×1
dependencies ×1
enable-if ×1
javascript ×1
jestjs ×1
latex ×1
layout ×1
loader ×1
matplotlib ×1
module ×1
package ×1
qt ×1
reusability ×1
scope ×1
setattribute ×1
sfinae ×1
tikz ×1
virtualenv ×1
void ×1