简体   繁体   中英

fix x and y axis format in log scale

Here's the code I used to generate the figure

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# generate data
x = np.linspace(0.01, 9.9, 15)
y = np.linspace(0.07, 0.7, 15)


matplotlib.rcParams['font.size'] = 20

# legend
matplotlib.rcParams['legend.frameon'] = False
matplotlib.rcParams['legend.fontsize'] = 'medium'

# ticks
matplotlib.rcParams['xtick.major.size'] = 10.0
matplotlib.rcParams['xtick.minor.size'] = 5.0
matplotlib.rcParams['xtick.major.width'] = 2.0
matplotlib.rcParams['xtick.minor.width'] = 2.0
matplotlib.rcParams['xtick.major.pad'] = 8.0

matplotlib.rcParams['ytick.major.size'] = 10.0
matplotlib.rcParams['ytick.minor.size'] = 5.0
matplotlib.rcParams['ytick.major.width'] = 2.0
matplotlib.rcParams['ytick.minor.width'] = 2.0
matplotlib.rcParams['ytick.major.pad'] = 8.0


fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(111)

plt.scatter(x, y, marker='o', color='k')

plt.xscale('log')
plt.xlim(xmin=0.005, xmax=10)
plt.yscale('log')
plt.ylim(ymin=0.07, ymax=0.7)

plt.xlabel('x')
plt.ylabel('y')

# x axis format
ax.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.2f"))

# y axis format
ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))
ax.yaxis.set_minor_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))

# borders 
plt.axhline(0.07, color='k', lw=2)
plt.axhline(0.7, color='k', lw=2)
plt.axvline(0.005, color='k', lw=2)
plt.axvline(10, color='k', lw=2)

plt.show()

在此处输入图片说明

I have the following questions

1- how can I fix the x-axis formatting so that the last 2 numbers should be written 1 and 10 (instead of 1.00 and 10.00)?

2- on the y-axis: I don't want all the numbers to be written, only few. How can I fix it to: 0.07, 0.1, 0.3, 0.6?

3- How can I remove the ticks from the corners?

Edit 1 (more details for question # 3)

If you look more carefully, in the figure below inside the blue rectangle, the corner is not smooth. That's because I plotted a line using plt.axhline and plt.axvline (to make the borders thick). The ticks on the corners overplotted by these lines makes the corners not smooth. My idea was to remove the ticks on the corners to make it smooth. Unless there is a smarter way that I am not aware of.

在此处输入图片说明

For your question #1, the following format setting is what you need:

import matplotlib.ticker as ticker

def customizedLogFormat(x,pos):
  decimalplaces = int(np.maximum(-np.log10(x),0))
  formatstring = '{{:.{:1d}f}}'.format(decimalplaces)      
  return formatstring.format(x)  

and then

ax.xaxis.set_major_formatter(ticker.FuncFormatter(customizedLogFormat))

For your question #2, I don't really know a smart way to achieve your goal... But after some tests, I think 'set_yticks' is one possible way to do that:

ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.2f"))
#ax.yaxis.set_minor_formatter(matplotlib.ticker.FormatStrFormatter("%.2f"))  
ax.set_yticks([0.07, 0.1, 0.3, 0.6])
ax.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) 

For your question #3, you can set the ticks position by the following code:

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

在此处输入图片说明

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