Matplotlib boxplot width in log scale

2024/10/18 13:38:08

I am trying to plot a boxplot with logarithmic x-axis. As you can see on the example below width of each box decreases because of the scale. Is there any way to make the width of all boxes same?

enter image description here

Answer

You can set a specific width of the box depending on the location on the plot. The boxplot's width argument allows to set different widths. To calculate the respective width, you need to transform the position to linear scale, add or subtract some linear width, and transform back to log scale. The difference between the two hence obtained values is the width of the bar to set.

The linear width w used here is of course somehow arbitrary; you need to select a good value for yourself.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)a = np.cumsum(np.random.rayleigh(150, size=(50,8)), axis=1)fig, ax=plt.subplots()positions=np.logspace(-0.1,2.6,8)w = 0.1
width = lambda p, w: 10**(np.log10(p)+w/2.)-10**(np.log10(p)-w/2.)ax.boxplot(a, positions=positions, widths=width(positions,w))
ax.set_xscale("log")
plt.show()

enter image description here

https://en.xdnf.cn/q/73327.html

Related Q&A

How to enable and disable Intel MKL in numpy Python?

I want to test and compare Numpy matrix multiplication and Eigen decomposition performance with Intel MKL and without Intel MKL. I have installed MKL using pip install mkl (Windows 10 (64-bit), Python …

getdefaultlocale returning None when running sync.db on Django project in PyCharm

OSX 10.7.3, PyCharm version 2.5 build PY 117.200Ill run through how I get the error:I start a new project Create a new VirtualEnv and select Python 2.7 as my base interpreter (leave inherit global pack…

Redirecting an old URL to a new one with Flask micro-framework

Im making a new website to replace a current one, using Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).The core functionality and many pages are the same. However by using…

python decimals - rounding to nearest whole dollar (no cents) - with ROUND_HALF_UP

Im trying to use Decimal.quantize() to achieve the following: -For any amount of money, expressed as a python decimal of default precision, I want to round it using decimal.ROUND_HALF_UP so that it has…

How to use pytest fixtures in a decorator without having it as argument on the decorated function

I was trying to use a fixture in a decorator which is intended to decorate test functions. The intention is to provide registered test data to the test. There are two options:Automatic import Manual im…

Including Python standard libraries in your distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Using watchdog of python to monitoring afp shared folder from linux

I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).I can mount shared folder by mount_afp, and installed watchdog python library to monitor a sha…

Fitting curve: why small numbers are better?

I spent some time these days on a problem. I have a set of data:y = f(t), where y is very small concentration (10^-7), and t is in second. t varies from 0 to around 12000.The measurements follow an est…

Fast numpy roll

I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll in a for loop to do so. But since I am calling this thousands of times, my code is really slow. Can you…

IndexError: fail to coerce slice entry of type tensorvariable to integer

I run "ipython debugf.py" and it gave me error message as belowIndexError Traceback (most recent call last) /home/ml/debugf.py in <module>() 8 fff = …