提问人:burcak 提问时间:9/30/2023 最后编辑:Trenton McKinneyburcak 更新时间:10/1/2023 访问量:45
如何在网格中显示轴刻度标签
How to show xaxis ticklabels in a grid
问:
如何在网格下方显示xaxis刻度标签?
ax.set_xticklabels([0.1, '', '', '', 0.5, '', '', '', '', 1.0], minor=True)
当我运行代码时,我得到下图。
如果我将代码行从
ax.set_xticks(np.arange(0, len(diameter_labels), 1))
自
ax.set_xticks(np.arange(0, len(diameter_labels), 1), minor=True)
这次我得到了下图。
事实上,我想得到下图。
import matplotlib.pyplot as plt
import os
import numpy as np
if __name__ == "__main__":
fig, ax = plt.subplots(figsize=(10, 3))
diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
row_labels = ['circle']
ax.grid(which="major", color="white", linestyle='-', linewidth=3)
ax.set_aspect(1.0)
for row_index, row_label in enumerate(row_labels):
for diameter_index, diameter_label in enumerate(diameter_labels):
circle=plt.Circle((diameter_index + 0.5, row_index + 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
ax.add_artist(circle)
ax.set_xlim([0, len(diameter_labels)])
ax.set_xticklabels([])
ax.set_xticks(np.arange(0, len(diameter_labels), 1))
ax.tick_params(axis='x', which='minor', length=0, labelsize=30)
ax.set_xticklabels([0.1, '', '', '', 0.5, '', '', '', '', 1.0], minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(
axis='x', # changes apply to the x-axis
which='major', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False) # labels along the bottom edge are off
ax.xaxis.set_label_position('top')
ax.set_xlabel('Test', fontsize=40, labelpad=5)
ax.set_ylim([0, len(row_labels)])
ax.set_yticklabels([])
ax.tick_params(axis='y', which='minor', length=0, labelsize=12)
ax.set_yticks(np.arange(0, len(row_labels), 1))
ax.tick_params(
axis='y',
which='major',
left=False)
ax.grid(which='major', color='black', linestyle='-', linewidth=1)
filename = 'test.png'
figureFile = os.path.join('/Users/burcakotlu/Desktop', filename)
fig.savefig(figureFile)
plt.close()
答:
1赞
Davide_sd
9/30/2023
#1
取代:
ax.set_xticks(np.arange(0, len(diameter_labels), 1), minor=True)
跟:
ticks = np.arange(0, len(diameter_labels) + 1)
labels = ticks.astype(str)
ax.set_xticks(ticks, labels)
我明白了。阅读以获取更多信息。help(ax.set_xticks)
编辑以满足评论:
然后,根据要实现的目标修改标签列表,如下所示:
ticks = np.arange(0, len(diameter_labels) + 1)
labels = [0.1, '', '', '', 0.5, '', '', '', '', 1.0, ""]
ax.set_xticks(ticks, labels)
编辑第三次是魅力:
fig, ax = plt.subplots(figsize=(10, 3))
diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
row_labels = ['circle']
ax.grid(which="major", color="white", linestyle='-', linewidth=3)
ax.set_aspect(1.0)
for row_index, row_label in enumerate(row_labels):
for diameter_index, diameter_label in enumerate(diameter_labels):
circle=plt.Circle((diameter_index + 0.5, row_index + 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
ax.add_artist(circle)
ax.set_xlim([0, len(diameter_labels)])
ax.set_ylim([0, len(row_labels)])
ax.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False) # labels along the bottom edge are off
ax.grid(which='major', color='black', linestyle='-', linewidth=1)
yticks = np.arange(0, len(row_labels))
ytickslabels = [] * len(yticks)
ax.set_yticks(yticks, ytickslabels)
xticks = np.arange(0, len(diameter_labels) + 1)
ax.set_xticks(xticks, xticks)
from matplotlib import ticker
ax.xaxis.set_major_formatter(ticker.NullFormatter()) # remove the major ticks
ax.yaxis.set_major_formatter(ticker.NullFormatter()) # remove the major ticks
ax.minorticks_on()
xminorticks = [0.5, 4.5, 9.5]
xminortickslabels = ["0.1", "0.5", "1"]
ax.set_xticks(xminorticks, xminortickslabels, minor=True)
评论
0赞
burcak
9/30/2023
我只想显示 0.1、0.5 和 1。
0赞
Davide_sd
9/30/2023
编辑答案。您只需要修改列表即可。确保长度与 相同。labels
labels
ticks
0赞
burcak
9/30/2023
但是 0.1 , 0.5 和 1.0 必须位于单元格的中间。我在问题中添加了一个新数字。
0赞
burcak
9/30/2023
为什么这并不容易?
0赞
Davide_sd
9/30/2023
完成,但你真的需要学习如何问一个问题:\有些细节很重要......
上一个:x 轴的斜体标签
评论
ax.set_xticklabels(...)
ax.set_xticks(tick_positions, tick_labels)