【问题标题】:Log-log density-colour plot in matplotlibmatplotlib 中的对数对数密度颜色图
【发布时间】:2017-02-27 15:24:13
【问题描述】:

我正在尝试使用 Matplotlib 2.0.0 版本创建具有给定数据并在两个轴 x、y 上使用对数刻度的密度图。我制作了以下代码,问题是对于日志情节案例没有给出正确的功能行为。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

init = 0.0
points = 500
final_value = 100
steep = (final_value-init)/points
list_values_x = np.arange(init,final_value,steep)
list_values_y = np.arange(init,final_value,steep)

#WE CREATE OUT DATA FILE

f1 = open("data.txt", "w")
for i in list_values_x:
    for j in list_values_y:
        f1.write( str(i) +" "+str(j)+" "+str(0.0001*(i**2+j**2)) +"\n")

f1.close()


#NOW WE OPEN THE FILE WITH THE DATA AND MAKE THE PLOT
x,y,temp = np.loadtxt('data.txt').T #Transposed for easier unpacking

nrows, ncols = points, points
grid = temp.reshape((nrows, ncols))

# LINEAR PLOT

fig1 = plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()),
           interpolation='nearest', cmap=cm.gist_rainbow)
plt.axis([x.min(), x.max(),y.min(),  y.max()])
plt.colorbar()
plt.suptitle('Example', fontsize=15)
plt.xlabel('x', fontsize=16)
plt.ylabel('y', fontsize=16)

plt.show()

# LOG-LOG PLOT

fig, (ax1) = plt.subplots(ncols=1, figsize=(8, 4))

ax1.imshow(grid, aspect="auto", extent=(1, 1e2, 1, 1e2), interpolation='nearest')
ax1.set_yscale('log')
ax1.set_xscale('log')
ax1.set_title('Example with log scale')



plt.show()

我用来制作情节的数据无关紧要,这只是一个例子。因此,第一个图以线性比例给出。第二个图是用对数刻度给出的,但很明显它是不正确的,两个图之间的行为是完全不同的,我使用的是相同的数据。此外,我不知道如何在 log-log 图中放置颜色条

知道为什么会这样吗?感谢您的关注。

PD:为了构建对数图,我使用了 (http://matplotlib.org/devdocs/users/whats_new.html#non-linear-scales-on-image-plots) 中给出的“图像图上的非线性比例”中出现的部分代码

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    imshow 中另外使用origin="lower" 时,将extent 关键字与extent=(xmin, xmax, ymin, ymax) 一起使用更有意义。您可能还想设置坐标轴的限制,因为自动功能不适用于对数刻度。

    这是完整的例子:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    
    points = 500
    init = 0.0
    final_value = 100
    steep = (final_value-init)/points
    x = np.arange(init,final_value,steep)
    y = np.arange(init,final_value,steep)
    X,Y = np.meshgrid(x,y)
    Z = 0.0001*(X**2+Y**2)
    
    fig, (ax, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
    # LINEAR PLOT
    im = ax.imshow(Z, extent=(x.min(), x.max(), y.min(), y.max() ),
               interpolation='nearest', cmap=cm.gist_rainbow, origin="lower")
    ax.set_title('lin scale')
    
    #make colorbar
    divider = make_axes_locatable(ax)
    ax_cb = divider.new_horizontal(size="5%", pad=0.05)
    fig.add_axes(ax_cb)
    fig.colorbar(im, cax = ax_cb, ax=ax)
    
    # LOG-LOG PLOT
    im1 = ax1.imshow(Z,  extent=(1, 1e2, 1, 1e2), 
               interpolation='nearest',cmap=cm.gist_rainbow, origin="lower")
    ax1.set_yscale('log')
    ax1.set_xscale('log')
    ax1.set_xlim([1, x.max()])
    ax1.set_ylim([1, y.max()])
    ax1.set_title('log scale')
    
    #make colorbar
    divider1 = make_axes_locatable(ax1)
    ax_cb1 = divider1.new_horizontal(size="5%", pad=0.05)
    fig.add_axes(ax_cb1)
    fig.colorbar(im1, cax = ax_cb1, ax=ax1)
    
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2022-11-25
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2013-08-14
      相关资源
      最近更新 更多