【问题标题】:plotting a numpy array : setting axis scale绘制一个 numpy 数组:设置轴比例
【发布时间】:2016-01-31 21:38:06
【问题描述】:

在从 numpy 数组中绘制图像时,我在尝试保持良好缩放的轴时遇到了一些麻烦。

这是我的代码:

def graphique(pixelisation):    
Couleur = {1 : "Vert", 2 : "Orange", 3 : "Rouge"}    
for c in range(1,2):
       liste = np.zeros((int(60//pixelisation),int(150//pixelisation)))
       for d in range(int(150//pixelisation)):
           for v in range(int(60//pixelisation)):
               my_input = {
                   "Distance" : d*pixelisation,
                   "Vitesse" :  v*pixelisation,
                   "Couleur" : c
                           }
               my_output = {
                   "Acceleration" : 0.0
                           }
               liste[v,d] = system.calculate(my_input, my_output).get("Acceleration")
       image = plt.matshow(liste)
       plt.title('a=f(d,v), feu '+Couleur.get(c))
       plt.xlabel('Distance(m)')
       plt.xaxis(0,150)
       plt.xticks(range(0,150,10))
       plt.ylabel('Vitesse(km/h)')
       plt.yticks(range(0,60,10))
       plt.colorbar(image)
       plt.show()    
return liste

问题是我创建了一个减小数组大小的参数。 但是如果我将大小除以 2,x 和 y 轴将从 0 变为正常范围的一半。

我对matplotlib数据库做了一些研究,但一无所获(实际上我并不了解所有功能)。

感谢您的帮助!

编辑: 例如 : A = np.zeros((10,10)) plt.matshow(A) plt.show()

会给我一个坐标轴从 0 到 10 的图像。

现在如果: A = np.zeros((100,100)) 如果我希望轴仍然显示从 0 到 10 的比例, 我该怎么办?

【问题讨论】:

    标签: python arrays numpy matplotlib


    【解决方案1】:

    您可以通过调用以下函数/方法之一来设置轴刻度:

    1. 使用轴对象的成员函数:

      ax.set_xlim(0, 10)
      ax.set_ylim(0, 10)
      

      其中ax 是坐标区对象(您可以通过ax = plt.gca() 获取当前对象)。

    2. 带模块功能plt.axis

      plt.axis([0, 10, 0, 10])
      

    您可以在documentation of matplotlib 中找到更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 2021-07-17
      • 2011-01-19
      • 1970-01-01
      • 2020-03-19
      • 2016-05-18
      • 2017-09-03
      • 2016-10-27
      相关资源
      最近更新 更多