Applying log, symmetric log, and logistic scales to your axes

To apply log, symmetric log and logistic scales we will go through the steps as follows:

  1. Plot the numbers from 0 to 10 against the power of 10, as shown:
# Log axes
plt.plot(nums, np.power(10,10*nums))
plt.gca().grid(True)

Following is the output of the preceding code:

In the preceding output, we can see that the plot is not very good for actually viewing these numbers, since the end of the data range is much bigger than the beginning of the data range.

  1. We can transform this into something that will fill the space, so that we are not left with an empty space, by using an algorithm. The easiest way to do this is to remove the plot keyword and call semilogy, as follows:
# Log axes
plt.semilogy(nums, np.power(10,10*nums))
plt.gca().grid(True)

The output of the preceding code is as follows:

In the preceding output, we can see the logarithm of the numbers and a perfectly straight line since the logarithm of the exponent is an identity.

  1. We can also get the logs for both axes by using plt.loglog. By doing this, we have a much better way of visualizing this information:
# Log axes
plt.loglog(nums, np.power(10,10*nums))
plt.gca().grid(True)

Following is the output of the preceding code:

  1. The scale can also be changed by using the set_yscale method, as shown in the following code:
# Log axes
plt.plot(nums, np.power(10,10*nums))
plt.gca().set_yscale('log')
plt.gca().grid(True)

We will get the following output:

For self-learning, you can use the set_xscale method, which will give a different output.
  1. Taking another example, in the following output, we have a wide range of data. Again, most of it looks like a straight line because the end is so much bigger than the beginning:
# Symlog axes
plt.plot(nums, np.power(10,nums)-100)
plt.gca().grid(True)

Following is the output of the preceding code:

  1. Looking at the log, we get the following:
# Symlog axes
plt.plot(nums, np.power(10,nums)-100)
plt.gca().set_yscale('log')
plt.gca().grid(True)

We will get the following output:

  1. For this case, where we have some negative and positive values and a huge dynamic range, Matplotlib provides the symlog scale, which actually splits into two log plots, one showing the positive values and the other showing the negative values. This is a nice way to actually visualize stuff that has a huge dynamic range and still contains a mixture of positive and negative values:
# Symlog axes
plt.plot(nums, np.power(10,nums)-100)
plt.gca().set_yscale('symlog')
plt.gca().grid(True)

Following is the output of the preceding code:

Finally, Matplotlib provides a third linear scale called the logistic or logit axis scale.

We have values between 0 and 1. Logistic scales are really good for dealing with probabilities. If there are probabilities that have mostly 0s in some places but peak up toward 1, you want to be able to visualize this more easily—the logistic or logit axis is really good for that.

  1. The one downside with the logistic axis are not well-scaled, so they tend to run right into each other, as shown here:
# Logistic axes
plt.plot(nums, nums/10)
plt.gca().set_yscale('logit')
plt.gca().grid(True)
# plt.gca().yaxis.set_minor_formatter(mpl.ticker.NullFormatter())

The preceding code gives the following output:

As we can see, the logistic axis gives a lot of information on the left and right-hand sides. It focuses at the resolution – we can think of it like a map projection where it is focusing on the amount of space at the high and low ends, around 0 and 1.

Hence, while dealing with this kind of scale, we have to get rid of the jumbled up values that are present on the y axis by disabling the formatter for the minor axis. By doing this, it keeps the ticks. They are often useful to see, and won't have that ugly mashup of text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset