简体   繁体   中英

Log x-scale in matplotlib box plot

I have an apparently simple question. Maybe it's just me misusing the library, but I can't make out what is the right syntax for it.

I have to make a boxplot from a set of data. I wish to put the x-axis in logarithmic scale, but just writing plt.xscale('log') makes the x-scale disappear.

Here is my code:

import matplotlib.pyplot as plt

# .. data analysis here ...

plt.boxplot(values, positions = pos, widths = w)
plt.xscale('log')
plt.yscale('log')
plt.show()

And the resulting image is:

在此处输入图片说明

Which is ok, apart from the fact that the x-axis is not appearing. Is there a simple solution to this problem?

Thank you! Any advice is greatly appreciated!

Try to use plt.autoscale() or set limits for x-axis manually:

import matplotlib.pyplot as plt
import numpy as np

# fake up some data
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)
spread = np.random.rand(50) * 100
center = np.ones(25) * 40
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
d2 = np.concatenate((spread, center, flier_high, flier_low), 0)
data.shape = (-1, 1)
d2.shape = (-1, 1)
data = [data, d2, d2[::2, 0]]

plt.figure()
plt.boxplot(data)
plt.xscale('log')
plt.yscale('log')
plt.autoscale(True)
plt.show()

在此处输入图片说明

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