简体   繁体   English

如何在 matplotlib plot 顶部显示刻度标签?

[英]how to show tick labels on top of matplotlib plot?

In matplotlib what is the way to have tick labels both at the bottom and in the top x axis?在 matplotlib 中,在底部和顶部 x 轴上都有刻度标签的方法是什么? I have searched a lot and still can't find how to do it.我已经搜索了很多,但仍然找不到如何做到这一点。

Sorry, I lied in the comments. 对不起,我在评论中撒了谎。 You can do this easily (but it seems to be badly documented) 你可以很容易地做到这一点(但似乎记录很差)

fig, ax = plt.subplots(1, 1)
ax.xaxis.set_tick_params(labeltop='on')

产量

You can do it with twiny() : 你可以用twiny()来做到这一点:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
X2tick_location= ax1.xaxis.get_ticklocs() #Get the tick locations in data coordinates as a numpy array
ax2.set_xticks(X2tick_location)
ax2.set_xticklabels(X2tick_location)
plt.show()

在此输入图像描述

Have a look to this question too for more elaborate plots. 对于更复杂的情节,也要看一下这个问题

This seems to be the standard way as of v3.5:这似乎是 v3.5 的标准方式:

fig, ax = plt.subplots()
ax.tick_params('x', top=True, labeltop=True)

顶部带有刻度和标签的图形

Note that the top keyword draws the ticks and the labeltop keyword draws the labels.请注意, top关键字绘制刻度,而labeltop关键字绘制标签。 Documentation here .文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM