对齐置信区间,一个在另一个下方

问题描述 投票:0回答:2

说我有一个熊猫数据帧,其中有3列值得关注:id(文本),minmax(浮点数)代表某个参数的置信区间的上下限( (以%为单位),例如:

id   min   max
 A   3.5   7.8
 B  11.35 13.25
 C   0.0   2.0

我想以ids在垂直轴上位于下方的图表(如下所示)表示这些数据,并且每条线表示相应id的置信区间的变化。

enter image description here

而且,当然,我的问题是:在Python中是否有(相当)简单的方法?

python dataframe confidence-interval
2个回答
0
投票

据我所知,没有图表可用于特别绘制置信区间。但是,您可以使用matplotlib.pyplot.barh()。代码如下。

数据帧准备:

matplotlib.pyplot.barh()

制作图表:

>>> import pandas as pd
>>> df = pd.DataFrame(
...     {
...         'id': ['A', 'B', 'C'],
...         'min': [3.5, 11.35, 0.0],
...         'max': [7.8, 13.25, 2.0]
...     }
... )
>>> df = df.sort_values(by='id', ascending=False)
>>> print(df)
  id    min    max
2  C   0.00   2.00
1  B  11.35  13.25
0  A   3.50   7.80

>>> import matplotlib >>> import matplotlib.pyplot as plt >>> plt.style.use('seaborn') >>> >>> plt.figure(figsize=(9.5, 2.5)) >>> plt.barh(df['id'], height=0.5, width=df['max']-df['min'], left=df['min']) >>> plt.title('Chart title') >>> plt.show()


0
投票

在python中,我们可以使用以下代码使用matplotlib库来解决它。

还附加了相同的输出图像。

enter image description here

import matplotlib.pyplot as plt import numpy as np import pandas as pd id = ["A","B","C"] min =[3.5,1.35,0.0] max =[7.8, 13.25, 2.0] data = [id,min,max] df = pd.DataFrame(data) df = df.T df.columns=["id","min","max"] for index, row in df.iterrows(): m2m = np.linspace( row['min'],row['max']) y_val = np.array([row["id"] for _ in range(len(m2m))]) plt.plot(m2m, y_val )

© www.soinside.com 2019 - 2024. All rights reserved.