简体   繁体   中英

How to change xticks and yticks of a log-log plot after ScalarFormatter?

I have this code:

 # Modules I import 

    import matplotlib
    if os.environ.get('DISPLAY','') == '':
        print('no display found. Using non-interactive Agg backend')
        matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from matplotlib.ticker import ScalarFormatter
    from pylab import *

# My Variables to plot

    ideal_walltime_list = [135.82, 67.91, 33.955, 16.9775, 8.48875]
    cores_plotting = [16, 32, 64, 128, 256]
    time_plotting = [135.82, 78.69, 50.62, 46.666, 42.473]

# My plotting part
    plt.figure()
    plt.scatter(cores_plotting, time_plotting, c='r', label='System wall time')
    plt.plot(cores_plotting, ideal_walltime_list, c='k', label='Ideal Wall time')

    plt.title('109bEdec_test')
    plt.ylabel('Wall time (s)')

    plt.xlabel('Cores')
    plt.legend(loc='upper right')
    plt.yscale('log')
    plt.xscale('log')

    ax = gca().xaxis
    ax.set_major_formatter(ScalarFormatter())
    ax.set_minor_formatter(ScalarFormatter())

    ay = gca().yaxis
    ay.set_major_formatter(ScalarFormatter())
    ay.set_minor_formatter(ScalarFormatter())
    plt.savefig('109bEdec_test' + '.png',dpi=1800)


    plt.show()

When I run this code my plot look like this:

在此处输入图片说明

However, I need my x-axis and y-axis to display the ticks corresponding to my cores_plotting variable and not all those numbers that have bad formatting. I have tried using:

plt.xticks(cores_plotting)
plt.yticks(cores_plotting)

But no success.

Also I tried:

plt.xticks(cores_plotting, ('16', '32', '64', '128', '256'))
plt.yticks(cores_plotting, ['16', '32', '64', '128', '256'])

But no success neither. Right now I just need to have the cores_plotting items as my X and Y ticks.

My python version is 3.6.5 and my Matplotlib version is 3.0.2.

Thanks!

You can first put the custom ticks which will act as the major ticks and then hide the minor ticks. You need to create an axis handle ax to access the minor ticks. Please check the strings you are using for your y-ticklabels.

fig, ax = plt.subplots()
plt.scatter(cores_plotting, time_plotting, c='r', label='System wall time')
plt.plot(cores_plotting, ideal_walltime_list, c='k', label='Ideal Wall time')

# Rest of your code here

ax.set_yticks(cores_plotting) 
ax.set_yticklabels(['16', '32', '64', '128', '256'])

ax.set_xticks(cores_plotting) 
ax.set_xticklabels(['16', '32', '64', '128', '256'])

for xticks in ax.xaxis.get_minor_ticks():
    xticks.label1.set_visible(False)
    xticks.set_visible(False)

for yticks in ax.yaxis.get_minor_ticks():
    yticks.label1.set_visible(False)
    yticks.set_visible(False)

Matplotlib version issue

It seems that the following commands does not work in matplotlib 3+ version and throws

TypeError: 'list' object is not callable` error.

In such case, use the above method to assign the ticks and the tick labels.

plt.xticks(cores_plotting, ['16', '32', '64', '128', '256']);
plt.yticks(cores_plotting, ['16', '32', '64', '128', '256'])

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM