matplotlib 如何在网格中显示xaxis ticklabels

xzlaal3s  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(105)

如何在网格下方显示xaxis ticklabels?

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()
gkn4icbw

gkn4icbw1#

更换:

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)

相关问题