Home > Back-end >  matplotlib: How do I get even ticks for N subplots?
matplotlib: How do I get even ticks for N subplots?

Time:04-17

I have a figure consisting of 5 different plots created using matplotlib which can be seen at the end of the post.

The ticks on the x-axis are all over the place. Some have 2, some have 3, and they're not even aligned.

My question: Is there some way, where I can consistently get, for each plot, for example xtick 0.1, 0.5, and 0.9? or just a way to tell matplotlib to ALWAYS show N xticks?

The following is my current code. I apologize if its horrible, I'm extremely new to matplotlib.

for runtime_data in loader.all_runtime_data:
    fig, axd = plt.subplot_mosaic([['topleft', 'topright'],['midleft', 'midright'],['bottom', 'bottom']])
    for key in runtime_data.keys():
        data = runtime_data[key] 
        # naive
        axd['topleft'].boxplot(data[loader.pingpongKey])
        axd['topleft'].tick_params(axis='both', labelsize=12)
        axd['topleft'].set_xticks([1], ['Pingpong'])
        axd['topleft'].set_ylabel('Time (ms)', fontsize=12)

        axd['topright'].boxplot(data[loader.threadringKey])
        axd['topright'].tick_params(axis='both', labelsize=12)
        axd['topright'].set_xticks([1], ['Threadring'])
        axd['topright'].set_ylabel('Time (ms)', fontsize=12)

        axd['midleft'].boxplot(data[loader.bigKey])
        axd['midleft'].tick_params(axis='both', labelsize=12)
        axd['midleft'].set_xticks([1], ['Big'])
        axd['midleft'].set_ylabel('Time (ms)', fontsize=12)

        axd['midright'].boxplot(data[loader.bangKey])
        axd['midright'].tick_params(axis='both', labelsize=12)
        axd['midright'].set_xticks([1], ['Bang'])
        axd['midright'].set_ylabel('Time (ms)', fontsize=12)

        axd['bottom'].boxplot(data[loader.reverseBangKey])
        axd['bottom'].tick_params(axis='both', labelsize=12)
        axd['bottom'].set_xticks([1], ['Reversebang'])
        axd['bottom'].set_ylabel('Time (ms)', fontsize=12)

        plt.tight_layout()
        plt.savefig('plots/boxplots/'   key   '-boxplot.pgf', bbox_inches='tight')

enter image description here

CodePudding user response:

To align the number of ticks on the y-axis, you can use set_yticks() to specify an arbitrary list or array of numbers.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(20200417)
all_data = [np.random.randint(0, val, size=100) for val in [10,100,500,1000]]

fig, axd = plt.subplot_mosaic([['topleft', 'topright'],['midleft', 'midright']])
axd['topleft'].boxplot(all_data[0])
axd['topleft'].tick_params(axis='both', labelsize=12)
axd['topleft'].set_xticks([1], ['Pingpong'])
axd['topleft'].set_ylabel('Time (ms)', fontsize=12)
axd['topleft'].set_yticks(np.arange(0,11,5))

axd['topright'].boxplot(all_data[1])
axd['topright'].tick_params(axis='both', labelsize=12)
axd['topright'].set_xticks([1], ['Threadring'])
axd['topright'].set_ylabel('Time (ms)', fontsize=12)
axd['topright'].set_yticks([0,50,100])

axd['midleft'].boxplot(all_data[2])
axd['midleft'].tick_params(axis='both', labelsize=12)
axd['midleft'].set_xticks([1], ['Big'])
axd['midleft'].set_ylabel('Time (ms)', fontsize=12)
axd['midleft'].set_yticks([0,250,500])

axd['midright'].boxplot(all_data[3])
axd['midright'].tick_params(axis='both', labelsize=12)
axd['midright'].set_xticks([1], ['Bang'])
axd['midright'].set_ylabel('Time (ms)', fontsize=12)
axd['midright'].set_yticks([0,500,1000])

plt.tight_layout()
#plt.savefig('plots/boxplots/'   key   '-boxplot.pgf', bbox_inches='tight')
plt.show()

enter image description here

CodePudding user response:

Use a

Or a

  • Related