设置手动X轴刻度小提琴图

时间:2019-01-22 10:07:51

标签: python-3.x matplotlib

我正在尝试使用matplotlib构建小提琴图。

虽然根据提供的示例here设置了手动X轴刻度,但我没有这样做。我在哪里错过了?

这是MWE

#!/usr/bin/env python3
import os
import numpy as np
import warnings
import matplotlib.pyplot as plt
import matplotlib.cbook
import matplotlib as mpl

warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
OUTPUT_PATH=os.getcwd() + "/"
# Dots per inch for figure.
DPI = 500

def test_plot():
    fig = plt.figure()
    vector_size=100
    bucket2 = np.random.rand(vector_size)
    bucket3 = np.random.rand(vector_size)
    bucket4 = np.random.rand(vector_size)
    bucket5 = np.random.rand(vector_size)
    bucket6 = np.random.rand(vector_size)

    pos = [1,2,3,4,5]
    data= [np.array(bucket2), np.array(bucket3), np.array(bucket4), np.array(bucket5), np.array(bucket6)]

    axes1 = fig.add_subplot(111)
    axes1.violinplot(data, pos, points=100, widths=0.7, showmeans=False, showextrema=True, showmedians=True)
    axes1.set_xlabel('x-axis')
    axes1.set_ylabel('y-axis')
    xticks_t =  ["",".1-.2", ".2-.3", ".3-.4", ".4-.5", ">.5"]
    axes1.set_xticklabels(xticks_t)
    axes1.set_xlim([0, 5])

    axes1.spines['right'].set_visible(False)
    axes1.spines['top'].set_visible(False)
    axes1.xaxis.set_ticks_position('bottom')
    axes1.yaxis.set_ticks_position('left')

    fig.tight_layout()
    file_name = 'test_violin.pdf'
    fig.savefig(OUTPUT_PATH + str(file_name), bbox_inches='tight', dpi=DPI, pad_inches=0.1)
    fig.clf()
    plt.close()
    pass

test_plot()

1 个答案:

答案 0 :(得分:1)

您可以在最后一个刻度上使用LaTeX表达式将>正确显示为

xticks_t =  ["",".1-.2", ".2-.3", ".3-.4", ".4-.5", r"$>.5$"]

并注释掉x轴限制# axes1.set_xlim([0, 5])

产生

enter image description here