【问题标题】:How to rescale an axis with matplotlib如何使用 matplotlib 重新缩放轴
【发布时间】:2021-10-21 03:01:00
【问题描述】:

我可以创建如下图:

import matplotlib.pyplot as plt    

image = [[0.0, 0.0, 0.0, 0.0, 0.0],
         [0.2, 0.0, 0.1, 0.0 ,0.0],
         [0.0, 0.0, 0.3, 0.0 ,0.0],
         [0.0, 0.0, 0.0, 0.0, 0.0],
         [0.0, 0.0, 0.0, 0.0, 0.0]]

print(image)
plt.imshow(image, cmap="plasma", interpolation='nearest')
plt.colorbar()
plt.xlabel("axis x")
plt.ylabel("axis y")
plt.show()

但是我怎样才能改变轴本身,即我想改变例如x 轴到不同的范围。例如,上面代码生成的绘图上的值 0 对应于值 -4.8573。上图中的值 '4' 对应的值是 12.443。

然后我想有一个刻度在 -5、0、10、15 左右的轴。我怎样才能做到这一点?

实轴值可以通过

x_real = a + x * b

【问题讨论】:

  • 仅供参考:彻底回答问题非常耗时。如果您的问题已解决,请通过接受最适合您的需求的解决方案表示感谢。 位于答案左上角的 / 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您有 15 岁以上的声誉,您也可以使用 / 箭头对答案的有用性进行投票。 如果解决方案不能回答问题,请发表评论What should I do when someone answers my question?。谢谢

标签: python matplotlib data-visualization visualization axis


【解决方案1】:

要自动插值,你可以这样做:

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

n=5 

image = [[0.0, 0.0, 0.0, 0.0, 0.0],
         [0.2, 0.0, 0.1, 0.0 ,0.0],
         [0.0, 0.0, 0.3, 0.0 ,0.0],
         [0.0, 0.0, 0.0, 0.0, 0.0],
         [0.0, 0.0, 0.0, 0.0, 0.0]]

print(image)
plt.imshow(image, cmap="plasma", interpolation='nearest')
plt.colorbar()

x = [37.59390426045407, 38.00530354847739, 38.28412244348653, 38.74871247986305, 38.73175910429809, 38.869008864244016, 39.188234404976555, 39.92835838352555, 40.881394113153334, 41.686136269465884]
y = [0.1305391767832006, 0.13764519613447768, 0.14573326951792354, 0.15090729309032114, 0.16355823707239897, 0.17327106424274763, 0.17749746339532224, 0.17310384614773594, 0.16545780437882962, 0.1604752704890856]


def ceil_power_of_10(n):
    exp = math.log(n, 10)
    exp = math.ceil(exp)
    return 10**exp


x0 = min(x)
x1 = max(x)

y0 = min(y)
y1 = max(y)

# Fill the 2D array
dx = (x1 - x0)/n
dy = (y1 - y0)/n

dx_steps = ceil_power_of_10(dx)
dy_steps = ceil_power_of_10(dy)

dx_steps_alpha = round((math.ceil(x1/dx_steps)*dx_steps) - (math.floor(x0/dx_steps)*dx_steps) )
dy_steps_alpha = round(((math.ceil(y1/dy_steps)*dy_steps) - (math.floor(y0/dy_steps)*dy_steps) ) / dy_steps)


x_new = np.linspace((math.floor(x0/dx_steps)*dx_steps), (math.ceil(x1/dx_steps)*dx_steps), dx_steps_alpha, endpoint=False)
y_new = np.linspace((math.floor(y0/dy_steps)*dy_steps), (math.ceil(y1/dy_steps)*dy_steps), dy_steps_alpha, endpoint=False)

labels_x = x_new
labels_x = [round(x,dx_steps) for x in labels_x]
positions_x = list(range(0, len(labels_x)))
labels_y = y_new
labels_y = [round(y/dy_steps) * dy_steps for y in labels_y]
positions_y = list(range(0, len(labels_y)))
# In the end, used to create a surface plot
plt.imshow(image, cmap="plasma", interpolation='nearest')
plt.xticks(positions_x, labels_x)
plt.yticks(positions_y, labels_y)
plt.xlabel("axis x")
plt.ylabel("axis y")
plt.show()

【讨论】:

    【解决方案2】:

    你的意思是这样的?

    import matplotlib.pyplot as plt    
    
    image = [[0.0, 0.0, 0.0, 0.0, 0.0],
             [0.2, 0.0, 0.1, 0.0 ,0.0],
             [0.0, 0.0, 0.3, 0.0 ,0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0]]
    
    print(image)
    plt.imshow(image, cmap="plasma", interpolation='nearest')
    plt.colorbar()
    
    positions = [0,1,2,3,4]
    labels = [-5, 0, 10, 15, 20]
    plt.xticks(positions, labels)
    plt.yticks(positions, labels)
    plt.xlabel("axis x")
    plt.ylabel("axis y")
    plt.show()
    

    【讨论】:

    • 是的,类似的。但是在这里我必须注意我显示的刻度,我的意思是我做 -5、0、10、20 还是 -5、-6、-7、-8、-9。 ...(在情节中自动完成)
    • 不知道你在问什么。让我们聊天,这样我就可以弄清楚你需要什么。
    • 我不知道如何开始聊天
    【解决方案3】:

    如果我理解正确,这应该会有所帮助

    view image

    import matplotlib.pyplot as plt    
    
    image = [[0.0, 0.0, 0.0, 0.0, 0.0],
             [0.2, 0.0, 0.1, 0.0 ,0.0],
             [0.0, 0.0, 0.3, 0.0 ,0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0]]
    
    print(image)
    plt.imshow(image, cmap="plasma", interpolation='nearest')
    plt.colorbar()
    plt.xlabel("axis x")
    plt.ylabel("axis y")
    #sets limes x
    plt.xlim([-5,15])
    #sets limes y
    plt.ylim([-5,15])
    plt.show()
    

    【讨论】:

    • 不,抱歉,您弄错了。我根本不想改变形象。只是,只有轴刻度和标记
    【解决方案4】:

    要重新调整 x 轴范围,您可以使用

    plt.xticks(ticks, labels)
    

    ticks:旧 xtick 位置列表。

    labels:放置在给定 ticks 位置的标签。

    所以,您只需要在 plt.show() 之前提供以下代码:

    plt.xticks(range(0, 5), range(-5, 16, 5))
    # range(0, 5): current range  
    # range(-5, 16, 5): new range
    # [0, 1, 2, 3, 4] -> [-5,  0,  5, 10, 15]
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    image = [[0.0, 0.0, 0.0, 0.0, 0.0],
             [0.2, 0.0, 0.1, 0.0 ,0.0],
             [0.0, 0.0, 0.3, 0.0 ,0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0]]
    
    print(image)
    plt.imshow(image, cmap="plasma", interpolation='nearest')
    plt.colorbar()
    plt.xlabel("axis x")
    plt.ylabel("axis y")
    plt.xticks(range(0, 5), range(-5, 16, 5))
    plt.show()
    

    Which product this image(click here)

    【讨论】:

      【解决方案5】:

      您应该计算轴的最小值、最大值和步长值:

      xmin = -4.8573
      xmax = 12.443
      dx = (xmax - xmin)/(np.shape(image)[0] - 1)
      ymin = -5
      ymax = 15
      dy = (ymax - ymin)/(np.shape(image)[1] - 1)
      

      然后,然后将这些值传递给imshowextent 参数:

      img = ax.imshow(image, cmap="plasma", interpolation='nearest', extent = [xmin - dx/2, xmax + dx/2, ymin - dy/2, ymax + dy/2])
      

      最后,设置坐标轴刻度:

      ax.set_xticks(np.linspace(xmin, xmax, (np.shape(image)[0])))
      ax.set_yticks(np.linspace(ymin, ymax, (np.shape(image)[1])))
      

      完整代码

      import matplotlib.pyplot as plt
      import numpy as np
      
      image = [[0.0, 0.0, 0.0, 0.0, 0.0],
               [0.2, 0.0, 0.1, 0.0 ,0.0],
               [0.0, 0.0, 0.3, 0.0 ,0.0],
               [0.0, 0.0, 0.0, 0.0, 0.0],
               [0.0, 0.0, 0.0, 0.0, 0.0]]
      
      print(image)
      
      xmin = -4.8573
      xmax = 12.443
      dx = (xmax - xmin)/(np.shape(image)[0] - 1)
      ymin = -5
      ymax = 15
      dy = (ymax - ymin)/(np.shape(image)[1] - 1)
      
      
      fig, ax = plt.subplots()
      
      img = ax.imshow(image, cmap="plasma", interpolation='nearest', extent = [xmin - dx/2, xmax + dx/2, ymin - dy/2, ymax + dy/2])
      plt.colorbar(img)
      ax.set_xlabel("axis x")
      ax.set_ylabel("axis y")
      ax.set_xticks(np.linspace(xmin, xmax, (np.shape(image)[0])))
      ax.set_yticks(np.linspace(ymin, ymax, (np.shape(image)[1])))
      
      plt.show()
      

      【讨论】:

      • 不不安静!我不想在 -4.86 处打勾。我想要一个新的刻度线多一点,只写“5”。而不是“-4.8635372347354327845236784”。这些值只是示例
      • 那么,你想要 x 轴上的哪个刻度?您的代码生成[0, 1, 2, 3, 4];你想要[..., ..., ...] 代替这个列表吗?
      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 2016-03-16
      • 1970-01-01
      相关资源
      最近更新 更多