简体   繁体   English

在对数刻度中固定x和y轴格式

[英]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)? 1-如何解决x轴格式问题,以便最后2个数字应分别写为1和10(而不是1.00和10.00)?

2- on the y-axis: I don't want all the numbers to be written, only few. 在y轴上的2:我不想写所有的数字,只写几个。 How can I fix it to: 0.07, 0.1, 0.3, 0.6? 如何将其固定为:0.07、0.1、0.3、0.6?

3- How can I remove the ticks from the corners? 3-如何去除角落的壁虱?

Edit 1 (more details for question # 3) 编辑1(问题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). 这是因为我使用plt.axhlineplt.axvline绘制了一条线(以使边框变粗)。 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: 对于问题1,您需要以下格式设置:

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: 对于您的问题2,我真的不知道实现目标的明智方法...但是经过一些测试,我认为'set_yticks'是实现此目标的一种可能方法:

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: 对于第3个问题,您可以通过以下代码设置刻度位置:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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