Reputation: 1047
I'm just started learning python and I got a little problem with my code I need to add annotation inside my graph. I've following some ref like this ref annotation
This my code:
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()
I got no error but, the results are nothing
I want the final results to return value from my data like this
can someone please help me?
Upvotes: 0
Views: 1352
Reputation: 80329
The problem is that to plot the data, the values are multiplied by 100, but for the annotations, there is no multiplication for the y position.
So, the solution would be to write:
ax1.annotate(str(j), xy=(i, j*100))
As there are many points close to each other, the plot rapidly looks too busy. The mplcursors library can come in handy. It shows an annotation while hovering over the plot.
Here is some demo code to get you started:
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()
Upvotes: 2