1

I'm trying to build a violin plot using matplotlib.

While setting the manual X-axis ticks based on the example provided here, I am failing to do so. Where am I missing out?

Here is a 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()
tandem
  • 2,040
  • 4
  • 25
  • 52

1 Answers1

3

You can use the LaTeX expressions for the last tick to correctly display > as

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

and comment out the x-axis limits # axes1.set_xlim([0, 5])

which produces

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71