简体   繁体   English

在matplotlib箱图中记录x比例

[英]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. 我希望将x轴设置为对数刻度,但是仅编写plt.xscale('log')会使x刻度消失。

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. 除了没有出现x轴之外,还可以。 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: 尝试使用plt.autoscale()或手动设置x轴限制:

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()

在此处输入图片说明

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

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