0
import pandas as pd
import matplotlib.pyplot as plt
import mplcursors

df = pd.DataFrame(
    {'Universe': ['Darvel', 'MC', 'MC', 'Darvel', 'MC', 'Other', 'Darvel'],
     'Value': [10, 11, 13, 12, 9, 7, 10],
     'Upper': [12.5, 11.3, 15.4, 12.2, 13.1, 8.8, 11.5],
     'Lower': [4.5, 9.6, 11.8, 6, 6.5, 5, 8]})
df['UpperError'] = df['Upper'] - df['Value']
df['LowerError'] = df['Value'] - df['Lower']

colors = ['r', 'g', 'b']

fig, ax = plt.subplots()
for i, universe in enumerate(df['Universe'].unique()):
    to_plot = df[df['Universe'] == universe]
    ax.scatter(to_plot.index, to_plot['Value'], s=16, c=colors[i])
    error = to_plot[['LowerError', 'UpperError']].transpose().to_numpy()
    ax.errorbar(to_plot.index, to_plot['Value'], yerr=error, fmt='o',
                markersize=0, capsize=6, color=colors[i])
    ax.scatter(to_plot.index, to_plot['Upper'], c='w', zorder=-1)
    ax.scatter(to_plot.index, to_plot['Lower'], c='w', zorder=-1)
    
mplcursors.cursor(hover=True)

plt.show()

これは私が望むことのほとんどを行いますが、次の変更が必要です。

  1. mplcursors カーソルがエラーバーと対話するのではなく、エラーバーの上部と下部にある非表示の散布図を含む散布図のみを操作します。

  2. y値を表示したいだけです。たとえば、最初のバーは、上に「12.5」、中央に「10.0」、下に「4.5」と表示する必要があります。

4

1 に答える 1