【问题标题】:Displaying 5 histograms on 1 axis - julia在 1 个轴上显示 5 个直方图 - julia
【发布时间】:2017-01-09 10:24:58
【问题描述】:

参考这个链接Displaying 3 histograms on 1 axis in a legible way - matplotlib在python的1个轴上显示3个直方图,我尝试在julia中做同样的事情,但dic函数无法识别。

这里是 julia 的代码。函数 dict 无法识别。

using PyCall
@pyimport matplotlib.pyplot as plt



a=vec(rand(1:1000,400,300))
b = vec(rand(200:700,400,300))
c = vec(rand(300:1200,400,300))

common_params = dict(bins=20, 
                     range=(-5, 5), 
                     normed="True")

plt.subplots_adjust(hspace=.4)
plt.subplot(311)
plt.title('Default')
plt.hist(a, **common_params)
plt.hist(b, **common_params)
plt.hist(c, **common_params)
plt.subplot(312)
plt.title('Skinny shift - 3 at a time')
plt.hist((a, b, c), **common_params)
plt.subplot(313)
common_params['histtype'] = 'step'
plt.title('With steps')
plt.hist(a, **common_params)
plt.hist(b, **common_params)
plt.hist(c, **common_params)

plt.savefig('3hist.png')
plt.show()

我尝试使用 : plotly 并且我也遇到了一些问题。这是使用plotly的代码

using Plotly

x0 = randn(500)
x1 = randn(500)+1


trace1 = [
  "x" => x0,
  "type" => "histogram"
]
trace2 = [
  "x" => x1,
  "type" => "histogram"
]
data = [trace1, trace2]
layout = ["barmode" => "stack"]
response = Plotly.plot(data, ["layout" => layout, "filename" => "stacked-histogram", "fileopt" => "overwrite"])
plot_url = response["url"]

返回的错误是:

WARNING: deprecated syntax "[a=>b, ...]" at /home/anelmad/Desktop/stage-inria/code/HTreeRBM.jl/my_tree/random_data/graph_try.jl:20.
Use "Dict(a=>b, ...)" instead.
ERROR: LoadError: MethodError: `convert` has no method matching convert(::Type{PlotlyJS.Plot{TT<:PlotlyJS.AbstractTrace}}, ::Array{Dict{ASCIIString,Any},1}, ::Dict{ASCIIString,Any})
This may have arisen from a call to the constructor PlotlyJS.Plot{TT<:PlotlyJS.AbstractTrace}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  PlotlyJS.Plot{T<:PlotlyJS.AbstractTrace}(::Array{T<:PlotlyJS.AbstractTrace,1}, ::Any)
  PlotlyJS.Plot(::PlotlyJS.AbstractTrace, ::Any)
  call{T}(::Type{T}, ::Any)

【问题讨论】:

    标签: matplotlib histogram julia plotly


    【解决方案1】:

    Julia 有自己的 matplotlib 接口,称为 PyPlot。 (你真的没想到你会简单地输入@pyimport,然后把python代码写到julia中,然后神奇地让它全部工作,是吗?:D)

    有关如何使用PyPlot 的示例,请参阅herehist 图特别需要一些特殊的语法来将其与 julia 原生的 hist 函数 (check the code for its example) 区分开来。

    这是您尽我所能翻译成 PyPlot 的代码(适用于 Julia v0.6.0)

    import PyPlot
    
    a = vec( rand( 1:1000,   400, 300 ))
    b = vec( rand( 200:700,  400, 300 ))
    c = vec( rand( 300:1200, 400, 300 ))
    
    common_params = Dict( :bins   => 20, 
                          :range  => (-100, 1300), 
                          :normed => true )
    
    PyPlot.subplots_adjust( hspace=.4 )
    
    PyPlot.subplot( 311 )
    PyPlot.title( "Default" )
    PyPlot.plt[:hist]( a; common_params... )
    PyPlot.plt[:hist]( b; common_params... )
    PyPlot.plt[:hist]( c; common_params... )
    
    PyPlot.subplot( 312 )
    PyPlot.title( "Skinny shift - 3 at a time" )
    PyPlot.plt[:hist]( (a, b, c); common_params... )
    
    PyPlot.subplot( 313 )
    common_params[:histtype] = "step"
    PyPlot.title( "With steps" )
    PyPlot.plt[:hist]( a; common_params... )
    PyPlot.plt[:hist]( b; common_params... )
    PyPlot.plt[:hist]( c; common_params... )
    
    PyPlot.savefig( "3hist.png" )
    PyPlot.show()  # PS, this is unnecessary in PyPlot. All commands show instantly
    

    我不确定您为什么选择 (-5,5) 的范围;这导致了第三个子图的问题。我在这里将其更改为更合理的内容,只是为了向您展示它的工作原理。

    结果如下:

    【讨论】:

      【解决方案2】:

      “julia”代码在我看来就像普通的 Python,这不是 PyCall 的工作方式。因此,dict() 不是 Julia 函数,但您可以编写以下代码来获取 Julia 字典:

      common_params = Dict("bins" => 20,  "range" => (-5, 5),  "normed" => true)
      

      您的 Plotly 示例的错误消息与您的代码不匹配。代码 using Plotly 引用的包与指示您正在使用 PlotlyJS 的错误消息不同(Plotly 和 PlotlyJS 是不同的包,PlotlyJS.jl 是推荐用于 julia v0.4 及更高版本的包)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-16
        • 2011-08-23
        • 2012-05-04
        • 1970-01-01
        • 2014-03-17
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        相关资源
        最近更新 更多