【问题标题】:python matplotlib: how to move the scale to the other side of the axis?python matplotlib:如何将刻度移动到轴的另一侧?
【发布时间】:2017-07-05 20:39:06
【问题描述】:

我有一个奇怪的东西,轴的比例显示在图中,例如:

还有我想要的: 如何将刻度移动到轴的另一侧?

x=range(len(ticks))
plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')
plt.xticks(x,ticks,rotation=45)
plt.xlabel('Messung')
plt.ylabel('$\Delta \phi [^\circ]$')
plt.legend()
plt.show()

【问题讨论】:

  • 那张图片不对怎么办?您能否包含有关所需行为的更多描述?是否要将 y 轴完全移动到图像的右侧?
  • 您希望 y 轴刻度位于图形的右侧,还是希望刻度和刻度标签位于图形框架的inside
  • 谢谢,请看新版
  • 我想要图框内的刻度是的。

标签: python matplotlib


【解决方案1】:

坐标轴的tick_params 可用于控制坐标轴标签和刻度位置。将方向设置为 in,以便它们指向图形。

如果您也想要不同的 y 轴范围和颜色,here 是一个很好的例子。

from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.tick_params(direction='in', length=6, width=2, colors='r', right=True, labelright='on')

plt.show()

【讨论】:

    【解决方案2】:

    您可以使用plt.tick_params() 来调整刻度的行为,文档可以在here 找到。

    对于您的示例,您希望刻度出现在图中。因此添加

    plt.tick_params(direction="in")
    

    到您的代码。示例:

    x=range(len(ticks))
    
    plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
    plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
    plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')
    
    plt.xticks(x,ticks,rotation=45)
    plt.xlabel('Messung')
    plt.ylabel('$\Delta \phi [^\circ]$')
    plt.legend()
    
    plt.tick_params(direction="in") # Set ticks inside the figure
    
    plt.show()
    

    您也可以在图的顶部和右侧显示刻度,如第二个屏幕截图所示,添加:

    plt.tick_params(direction="in",top="on",right="on")
    

    如果您想让 Python 脚本中的所有个图形都具有这种行为,那么您可以在脚本顶部添加以下内容(this 可能会感兴趣):

    import matplotlib
    
    matplotlib.rcParams['xtick.direction'] = "in"
    matplotlib.rcParams['ytick.direction'] = "in"
    

    这将使您不必为每个图形调用plt.tick_params(),如果您生成大量图形,这将很有帮助。

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2011-09-18
      • 2016-06-03
      相关资源
      最近更新 更多