【问题标题】:Adding a annotation above point matplotlib在点 matplotlib 上方添加注释
【发布时间】:2020-03-25 18:04:17
【问题描述】:

我刚开始学习 python,我的代码有点问题,我需要在我的图表中添加注释。 我关注了一些像这样的参考ref annotation

这是我的代码:

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


data = {
'jenis' : ['set1', 'set2', 'set3', 'set4', 'set5'],
'data1' :[0.80443, 0.84176, 0.84278, 0.82316,0.82260],
'data2' :[0.71956, 0.77691, 0.77279, 0.74522,0.74747],
'data3' :[0.84256, 0.83268, 0.84152, 0.84204,0.83775],
'data4' :[0.71956, 0.77691, 0.77279, 0.74522,0.74747],
'data5' :[0.80320, 0.83787, 0.83933, 0.82087,0.82008],
'data6' :[0.71956, 0.77043, 0.76772, 0.74286,0.74432],
'data7' :[0.83641, 0.83009, 0.83847, 0.83743,0.83333],
'data8' :[0.71956, 0.77043, 0.76772, 0.74286,0.74432],
}

df = pd.DataFrame.from_dict(data); df
fig = plt.figure(figsize=(8,4))
ax1=plt.subplot(111)
ax1.plot(df.jenis, [ x * 100 for x in df.data1 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data2 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data3 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data4 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data5 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data6 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data7 ],'s-')
ax1.plot(df.jenis, [ x * 100 for x in df.data8 ],'s-')
plt.ylim([60, 100])
plt.legend(
    ['Skenario 1',
     'Skenario 2',
     'Skenario 3',
     'Skenario 4',
     'Skenario 5',
     'Skenario 6',
     'Skenario 7',
     'Skenario 8'
     ],
    loc='best'
    )

for i,j in df.data1.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data2.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data3.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data4.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data5.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data6.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data7.items():
    ax1.annotate(str(j), xy=(i, j))
for i,j in df.data8.items():
    ax1.annotate(str(j), xy=(i, j))

plt.ylabel('Akurasi')
plt.title('Performance Measurements')
plt.show()

我没有错误,但是结果什么都没有

我希望最终结果从我的数据中返回值,如下所示

有人可以帮我吗?

【问题讨论】:

  • 将 j 乘以 100 可能会有所帮助,例如 ax1.annotate(str(j), xy=(i, j*100))。此外,您迫切需要合并for 循环,如for data_col in df.columns[1:]: `for i,j in df[data_col].items():``....

标签: python numpy matplotlib


【解决方案1】:

问题是绘制数据时,值乘以 100,但对于注释,y 位置没有乘法。

所以,解决办法是这样写:

ax1.annotate(str(j), xy=(i, j*100))

由于有很多点彼此靠近,所以剧情很快就显得太忙了。 mplcursors 库可以派上用场。它在将鼠标悬停在绘图上时显示注释。

这里有一些演示代码可以帮助您入门:

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

data = {'jenis': ['set1', 'set2', 'set3', 'set4', 'set5'],
        'data1': [0.80443, 0.84176, 0.84278, 0.82316, 0.82260],
        'data2': [0.71956, 0.77691, 0.77279, 0.74522, 0.74747],
        'data3': [0.84256, 0.83268, 0.84152, 0.84204, 0.83775],
        'data4': [0.71956, 0.77691, 0.77279, 0.74522, 0.74747],
        'data5': [0.80320, 0.83787, 0.83933, 0.82087, 0.82008],
        'data6': [0.71956, 0.77043, 0.76772, 0.74286, 0.74432],
        'data7': [0.83641, 0.83009, 0.83847, 0.83743, 0.83333],
        'data8': [0.71956, 0.77043, 0.76772, 0.74286, 0.74432]}
df = pd.DataFrame.from_dict(data)

fig = plt.figure(figsize=(8, 4))
ax1 = plt.subplot(111)
for i, data_col in enumerate( df.columns[1:]):
    ax1.plot(df.jenis, [x * 100 for x in df[data_col]], 's-', label=f'Skenario {i+1}')
plt.ylim([60, 100])

# optionally show annotations
# for data_col in df.columns[1:]:
#     for i,j in df[data_col].items():
#         ax1.annotate(str(j), xy=(i, j*100))

mplcursors.cursor(hover=True)

plt.ylabel('Akurasi')
plt.title('Performance Measurements')
plt.legend(loc='best')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多