【问题标题】:Setting axis values in numpy/matplotlib.plot在 numpy/matplotlib.plot 中设置轴值
【发布时间】:2016-08-24 11:51:23
【问题描述】:

我正在学习numpy。我希望绘制不同温度下的普朗克定律图,因此分别有两个np.arrays、Tl 用于温度和波长。

import scipy.constants as sc
import numpy as np
import matplotlib.pyplot as plt

lhinm = 10000                         # Highest wavelength in nm
T = np.linspace(200, 1000, 10)        # Temperature in K
l = np.linspace(0, lhinm*1E-9, 101)   # Wavelength in m
labels = np.linspace(0, lhinm, 6)     # Axis labels giving l in nm

B = (2*sc.h*sc.c**2/l[:, np.newaxis]**5)/(np.exp((sc.h*sc.c)/(T*l[:, np.newaxis]*sc.Boltzmann))-1)

for ticks in [True, False]:
    plt.plot(B)
    plt.xlabel("Wavelength (nm)")
    if ticks:
        plt.xticks(l, labels)
        plt.title("With xticks()")
        plt.savefig("withticks.png")
    else:
        plt.title("Without xticks()")
        plt.savefig("withoutticks.png")
    plt.show()

我想用波长 (nm) 标记 x 轴。如果我不调用plt.xitcks(),x 轴上的标签似乎是数组 B 中的索引(其中包含计算值)。

我已经看到答案 7559542,但是当我调用 plt.xticks() 时,所有值都在轴的左侧被挤压,而不是沿着它均匀分布。

那么定义我自己的一组值(在这种情况下是l 中的值的子集)并将它们放在轴上的最佳方法是什么?

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    问题是您没有将波长值提供给plt.plot(),因此 Matplotlib 默认将索引放入水平轴上的数组中。快速解决方案:

    plt.plot(l, B)
    

    没有明确设置刻度标签,这会给你这个:

    当然,此图中横轴上的值实际上以米为单位,而不是纳米(尽管有标签),因为您作为第一个参数传递给plot()(即数组l)的值是以米为单位。这就是xticks() 的用武之地。双参数版本xticks(locations, labels) 将标签放置在x 轴上的相应位置。例如,xticks([1], 'one') 会在位置 x=1 处放置一个标签“一”,如果该位置在图中。

    但是,它不会更改轴上显示的范围。在您的原始示例中,您对 xticks() 的调用在 10-9 等坐标处放置了一堆标签,但它并没有改变轴范围,仍然是 0 到 100。难怪所有人标签被压向左边。

    您需要做的是调用xticks() 并提供您要放置标签的点以及所需的标签文本。你这样做的方式,xticks(l, labels),除了l 的长度为 101 和 labels 的长度只有 6 之外,它只使用 l 的前 6 个元素。要解决这个问题,您可以执行类似的操作

    plt.xticks(labels * 1e-9, labels)
    

    1e-9 的乘法从纳米(您想要显示的内容)转换为米(这是 Matplotlib 在绘图中实际使用的坐标)。

    【讨论】:

      【解决方案2】:

      您可以将 x 值提供给 plt.plot,并让 matplotlib 负责设置刻度标签。

      在您的情况下,您可以绘制 plt.plot(l, B),但您仍然有 m 中的刻度,而不是 nm。

      因此,您可以在绘图之前(或在绘图期间)将您的 l 数组转换为 nm。这是一个工作示例:

      import scipy.constants as sc
      import numpy as np
      import matplotlib.pyplot as plt
      
      lhinm = 10000                         # Highest wavelength in nm
      T = np.linspace(200, 1000, 10)        # Temperature in K
      l = np.linspace(0, lhinm*1E-9, 101)   # Wavelength in m
      l_nm = l*1e9                          # Wavelength in nm
      labels = np.linspace(0, lhinm, 6)     # Axis labels giving l in nm
      
      B = (2*sc.h*sc.c**2/l[:, np.newaxis]**5)/(np.exp((sc.h*sc.c)/(T*l[:, np.newaxis]*sc.Boltzmann))-1)
      
      plt.plot(l_nm, B)
      # Alternativly:
      # plt.plot(l*1e9, B)
      plt.xlabel("Wavelength (nm)")
      plt.title("With xticks()")
      plt.savefig("withticks.png")
      plt.show()
      

      【讨论】:

        【解决方案3】:

        您需要在xtick 使用相同大小的列表。尝试将轴值与绘图值分开设置,如下所示。

        import scipy.constants as sc
        import numpy as np
        import matplotlib.pyplot as plt
        
        lhinm = 10000                         # Highest wavelength in nm
        T = np.linspace(200, 1000, 10)        # Temperature in K
        l = np.linspace(0, lhinm*1E-9, 101)   # Wavelength in m
        ll = np.linspace(0, lhinm*1E-9, 6)    # Axis values
        labels = np.linspace(0, lhinm, 6)     # Axis labels giving l in nm
        
        B = (2*sc.h*sc.c**2/l[:, np.newaxis]**5)/(np.exp((sc.h*sc.c)/(T*l[:, np.newaxis]*sc.Boltzmann))-1)
        
        for ticks in [True, False]:
            plt.plot(B)
            plt.xlabel("Wavelength (nm)")
            if ticks:
                plt.xticks(ll, labels)
                plt.title("With xticks()")
                plt.savefig("withticks.png")
            else:
                plt.title("Without xticks()")
                plt.savefig("withoutticks.png")
            plt.show()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-03
          • 2014-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多