【问题标题】:Add colorbar to subplot in Julia PyPlot将颜色条添加到 Julia PyPlot 中的子图
【发布时间】:2019-05-15 10:42:03
【问题描述】:

我尝试将this answer 中的 Python 代码翻译成 Julia,如下所示。

using PyPlot
# normal distribution center at x=0 and y=5
x,y = randn(100000), randn(100000) .+ 5
# plot 2d histogram with color bar
fig, ax = plt.subplots()
h = ax.hist2d(x, y, bins=40)
plt.colorbar(h[3], ax=ax)

备注:这里,.+5 表示通过5 逐元素添加向量。在 Julia 中,可以直接使用plt

但是,我收到了错误消息。由于我很少用 Python 编写程序,因此错误消息并没有让我远离解决方案。

PyError ($(Expr(:escape, :(ccall(#= /home/vin100/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'AttributeError'>
AttributeError("'numpy.ndarray' object has no attribute 'autoscale_None'",)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py", line 2100, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py", line 2129, in colorbar
    cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py", line 1566, in colorbar_factory
    cb = Colorbar(cax, mappable, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py", line 1072, in __init__
    mappable.autoscale_None()
in top-level scope at base/none
in  at base/none
in #call#111 at PyCall/ttONZ/src/pyfncall.jl:89 
in _pycall! at PyCall/ttONZ/src/pyfncall.jl:11
in _pycall! at PyCall/ttONZ/src/pyfncall.jl:29
in __pycall! at PyCall/ttONZ/src/pyfncall.jl:44
in macro expansion at PyCall/ttONZ/src/exception.jl:84 
in pyerr_check at PyCall/ttONZ/src/exception.jl:64 
in pyerr_check at PyCall/ttONZ/src/exception.jl:60 

如何在 Julia 中为 PyPlot 制作的 2D 直方图添加彩条?

【问题讨论】:

    标签: python matplotlib julia


    【解决方案1】:

    原来我忘记了 Python 和 Julia 中的数组索引之间的区别。对于前者和后者,数组分别从索引 0 和 1 开始。因此,在上述代码块中的索引3 中添加一个就足够了

    using PyPlot
    # normal distribution center at x=0 and y=5
    x,y = randn(100000), randn(100000) .+ 5
    # plot 2d histogram with color bar
    fig, ax = plt.subplots()
    h = ax.hist2d(x, y, bins=40)
    plt.colorbar(h[4], ax=ax)
    

    为了得到一个带有彩条的二维直方图。

    【讨论】:

      猜你喜欢
      • 2016-06-22
      • 2020-11-13
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多