matplotlib:如何从辅助轴中删除刻度线的价格?

时间:2020-01-21 14:08:20

标签: python matplotlib

我有一个用于图形的代码,并且我不想在上轴和右轴上使用值和刻度。

import matplotlib.pyplot as plt
fig, ax = plt.subplots()


#Set axis labels
ax.set_xlabel('NEGATIVE')
ax.set_ylabel('HAPPY')
ax2 = ax.secondary_xaxis('top')
ax2.set_xlabel('POSITIVE')
ax2 = ax.secondary_yaxis('right')
ax2.set_ylabel('SAD')


#Remove ticks/values
ax.set_yticklabels([])
ax.set_xticklabels([])
ax.set_yticks([])
ax.set_xticks([])
ax2.set_yticklabels([])
ax2.set_xticklabels([])
ax2.set_yticks([])
ax2.set_xticks([])

#Show graph
plt.show()

它显示如下:image of graph

2 个答案:

答案 0 :(得分:3)

使用Add来操纵轴刻度和标签:

Remove

一条评论询问如何仅关闭顶部和左侧的刻度线和标签。这将是

BindingList

答案 1 :(得分:1)

有趣的是为什么SecondaryAxis不接受滴答参数,但是让我们使用twinxtwiny

import matplotlib.pyplot as plt
fig, ax = plt.subplots()


#Set axis labels
ax.set_xlabel('NEGATIVE')
ax.set_ylabel('HAPPY')
ax2x = ax.twiny()
ax2.set_yticks([])
ax2x.set_xlabel('POSITIVE')
ax2y = ax.twinx()
ax2y.set_ylabel('SAD')


ax2x.set_xticks([])
ax2y.set_yticks([])
#Show graph
plt.show()

输出:

enter image description here