(Python) Log transformation of image axes in Matplotlib

For data with a large span and discrete distribution, log transformation is often used to abbreviate the gap, and the effect is better when presented on the graph. For example, when plotting transcriptome expression data, the values ​​after log transformation are often plotted. In matplotlib, log conversion of data is supported during drawing. According to the requirements of log conversion, the following three functions are used.

1. loglog, perform log transformation on the x-axis and y-axis values ​​at the same time

2. semilogx, only performs log transformation on the x-axis value, leaving the y-axis value unchanged

3. semilogy, only perform log transformation on the y-axis value, and the x-axis value remains unchanged

The above three functions are actually plot functions, but the corresponding data are automatically log-converted before drawing, so the parameters of the plot function are applicable to these functions. Let’s take a closer look at the usage.

1. loglog

First, build a scatter plot with the x-axis and y-axis data both being powers of 10. The code is as follows
 

import matplotlib.pyplot as plt
import numpy as np
data = np.array([1, 2, 3, 4])
power_x = np.power(10 , data)
power_y = np.power(10 , data)
plt.plot(power_x, power_y)

The output is as follows

Through the loglog function, the data of the x-axis and the y-axis can be log transformed at the same time. The usage is as follows

plt.loglog(power_x, power_y)

The output is as follows

It can be seen from the effect that the values ​​after log10 conversion are used for drawing, and the corresponding labels are marked by exponentiation.

2. semilogx

The semilogx function only performs log conversion on the x-axis value. Let’s first look at the effect without conversion. The code is as follows

plt.plot(power_x, data)

The output is as follows

Only perform log transformation on the x-axis value, the code is as follows

plt.semilogx(power_x, data)

The output is as follows

3. semilogy

The semilogy function only performs log conversion on the y-axis value. Let’s first look at the effect without conversion. The code is as follows

plt.plot(data, power_y)

The output is as follows

Only perform log transformation on the y-axis value, the code is as follows

plt.semilogy(data, power_y)

The output is as follows

In addition to the above basic usage, this function also has the following 3 exclusive parameters:

1. base, specifies the value of the logarithm, the default value is 10, that is, log10 conversion

2. subs, set the position of minor ticks, the default value is None

3. Nonpositive, processing of non-negative values, because only positive numbers can take log, if the original value is a negative value, there are two processing methods at this time, the first is to discard this point, which is also the default processing method, corresponding to this The value of the parameter is mask. This point is not displayed in the figure. The second method is to adjust this value to the nearest positive number, and the corresponding value of this parameter is clip.

In order to facilitate the precise specification of the x-axis and y-axis, the above parameters have two versions: x-axis and y-axis. Taking base as an example, there are two specific parameters: basex and basey. The usage is as follows
 

plt.loglog(power_x, power_y, basex=2)

The output is as follows

Through the log series of functions, data can be flexibly log transformed.

Guess you like

Origin blog.csdn.net/qq_40728667/article/details/133909921