Pandas secondary y axis for boxplots

2024/10/12 22:32:30

I'd like to use a secondary y-axis for some boxplots in pandas, but it doesn't seem available.

import numpy as np
import pandas as pddata = np.random.random((10, 5))
data[:,-1] += 10  # offset one column
df = pd.DataFrame(data)

Now, using the default line plot it's easy enough to plot to a second y-axis:

df.plot(kind='line', secondary_y=[4])

normal line plot

But if I use boxplot style, it doesn't work:

df.plot(kind='box', secondary_y=[4])

box plot without second yaxis

Is there any way (maybe through matplotlib) I can get pandas to plot 2 axes for boxplot? Using the boxplot() method would be preferable, but I'll take plot(). Thanks.

Answer

Actually not that hard with matplotlib.

In [22]: fig, ax = plt.subplots()In [23]: df[[0, 1, 2, 3]].plot(kind='box', ax=ax)
Out[23]: <matplotlib.axes._subplots.AxesSubplot at 0x4890978>In [24]: ax2 = ax.twinx()In [25]: ax2.boxplot(df[4], positions=[4])In [27]: ax.set_xlim(0, 5)
Out[27]: (0, 5)

There's still some cleanup with the styling, ticklabels etc. But that should get you started.

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

Related Q&A

Fixing Negative Assertion for end of string

I am trying to accept a capture group only if the pattern matches and there is not a specific word before the end of the group. Ive tried a # of approaches and none seem to work, clearly Im not getting…

Two Sorted Arrays, sum of 2 elements equal a certain number

I was wondering if I could get some help. I want to find an algorithm that is THETA(n) or linear time for determining whether 2 numbers in a 2 sorted arrays add up to a certain number.For instance, let…

I cant seem to install numpy

I tried to install numpy, but whenever I start my program, I get these messages.Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, an…

Using slices in Python

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:from pandas import * from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_mode…

Elasticsearch delete_by_query wrong usage

I am using 2 similar ES methods to load and delete documents:result = es.search(index=users_favourite_documents,doc_type=favourite_document,body={"query": {"match": {user: user}}})A…

SQLAlchemy: Lost connection to MySQL server during query

There are a couple of related questions regarding this, but in my case, all those solutions is not working out. Thats why I thought of asking again. I am getting this error while I am firing below quer…

row to columns while keeping part of dataframe, display on same row

I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.Resulting Dataframe:ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 1 …

SQLAlchemy InvalidRequestError when using composite foreign keys

My table relationships in SQLAlchemy have gotten quite complex, and now Im stuck at this error no matter how I configure my relationship.Im a bit new to SQLAlchemy so Im not sure what Im doing wrong, b…

google search by google api in r or python

I want to search some thing (ex:"python language") in google by python or R and it will give me the list of links for that google search like:https://en.wikipedia.org/wiki/Python_(programming…

NumPy Wont Append Arrays

Im currently working on a neural network to play Rock-Paper-Scissors, but Ive run into an enormous issue.Im having the neural network predict what will happen next based on a history of three moves, wh…