获取图中矩形的高度

Get the height of the rectangles in a plot

我使用以下代码[2]获得了下图1。从第一行可以看出,我根据标准偏差值给出了矩形的高度。但我不知道如何获得相应矩形的高度。例如,给定蓝色矩形,我想 return 包含它的 2 个间隔大约为 128.8 和 130.6。我该怎么做?

[2] 我使用的代码如下:

import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import numpy as np

dfLunedi = pd.read_csv( "0.lun.csv", encoding = "ISO-8859-1", sep = ';')

dfSlotMean = dfLunedi.groupby('slotID', as_index=False).agg( NLunUn=('date', 'nunique'),NLunTot = ('date', 'count'), MeanBPM=('tempo', 'mean'), std = ('tempo','std') )
#print(dfSlotMean)

dfSlotMean.drop(dfSlotMean[dfSlotMean.NLunUn < 3].index, inplace=True)
    
df = pd.DataFrame(dfSlotMean)

df.to_csv('1.silLunedi.csv', sep = ';', index=False)

print(df)

bpmMattino = df['MeanBPM']
std = df['std']
listBpm = bpmMattino.tolist()

limInf = df['MeanBPM'] - df['std']
limSup = df['MeanBPM'] + df['std']

tick_spacing = 1
fig, ax = plt.subplots(1, 1)
for _, r in df.iterrows():
    #
    ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2, linewidth = r['std'] )
    #ax.plot([r['slotID'], r['slotID']+1], [r['MeanBPM']]*2, linewidth = r['std'])
    ax.xaxis.grid(True)
    ax.yaxis.grid(True)
    ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
    ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
    

这是 csv 的内容:

    slotID  NMonUnique NMonTot     MeanBPM        std      
0        7      11       78  129.700564  29.323091  
2       11       6       63  123.372397  24.049397  
3       12       6       33  120.625667  24.029006    
4       13       5       41  124.516341  30.814985    
5       14       4       43  118.904512  26.205309    
6       15       3       13  116.380538  24.336491    
7       16       3       42  119.670881  27.416843  
8       17       5       40  125.424125  32.215865    
9       18       6       45  130.540578  24.437559  
10      19       9       58  128.180172  32.099529    
11      20       5       44  125.596045  28.060657 

如果定义高度为标准差,中心在均值,则区间为[mean-(std/2);每个矩形的均值+(std/2)] 对吗?矩形重叠是故意的吗?如果不是,我认为这是您使用线宽来调整错误的矩形大小。如果该图可以可视化不同类别的均值和方差,则像箱线图或雨云图之类的东西可能会更好。

我建议不要使用 linewidth 来显示与您的数据相关的任何内容。原因是 linewidth 以“点”衡量(参见 matplotlib documentation),其大小与您在其中绘制数据的 xy-space 无关。要查看此内容实际上,尝试使用不同的线宽绘图并更改 plotting-window 的大小。线宽不会随轴改变。

相反,如果您确实想要一个矩形,我建议使用 matplotlib.patches.Rectangle。文档中很好地 example 说明了如何执行此操作,我还在下面添加了一个更短的示例。

要为矩形赋予不同的颜色,您可以按照此处 here and simply get a random tuple with 3 elements and use that for the color. Another option is to take a list of colors, for example the TABLEAU_COLORSmatplotlib.colors 进行操作,并从该列表中获取连续的颜色。后者可能更适合测试,因为每个 运行 的矩形都会获得相同的颜色,但请注意 TABLEAU_COLORS 中只有 10 种颜色,因此如果超过 10 种颜色,则必须循环10 个矩形。

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
import random

x = 3
y = 4.5
y_std = 0.3

fig, ax = plt.subplots()
for i in range(10):
    c = tuple(random.random() for i in range(3))
    # The other option as comment here
    #c = mcolors.TABLEAU_COLORS[list(mcolors.TABLEAU_COLORS.keys())[i]]
    rect = ptc.Rectangle(xy=(x, y-y_std), width=1, height=2*y_std, color=c)
    ax.add_patch(rect)
ax.set_xlim((0,10))
ax.set_ylim((0,5))
plt.show()