I have created a list of markers in which each marker is used to show the max value of that curve. (4 curves -> 4 markers)
Somehow I cannot get all the markers to appear in the plot. It seems only the first marker in the for loop is plotted.
JavaScript
x
38
38
1
import pandas as pd
2
from matplotlib import pyplot as plt
3
import numpy as np
4
5
6
df0 = pd.DataFrame(np.random.randint(0,1000,size=(18, 4)), columns=list('ABCD'))
7
#df0 = pd.read_excel("est.xlsx")
8
df0.plot()
9
mylist = list(df0)
10
# df=df.astype(float)
11
12
mrkr = ['o', '.', 'v', 'x']
13
mrkr_df = pd.DataFrame(mrkr)
14
15
idx_mx_vals = []
16
for i in range(len(mylist)):
17
if df0[mylist[i]][0] > 0:
18
idx_mx_vals.append(df0[mylist[i]].idxmax())
19
else:
20
idx_mx_vals.append(df0[mylist[i]].idxmin())
21
22
mx_vals = []
23
for i in range(len(mylist)):
24
if df0[mylist[i]][0] > 0:
25
mx_vals.append(df0[mylist[i]].max())
26
else:
27
mx_vals.append(df0[mylist[i]].min())
28
29
d = {}
30
for i in range(len(mylist)):
31
d[df0.columns[i]] = [idx_mx_vals[i], mx_vals[i], mrkr[i]]
32
df = pd.DataFrame(d)
33
print(df)
34
35
for col in range(len(mrkr_df.columns)):
36
plt.plot(df.iloc[0, col], df.iloc[1, col], marker=mrkr_df.iloc[2, col],
37
markersize=20)
38
Advertisement
Answer
You can greatly simplify your code, use idxmax
directly on the absolute values as a vectorial call. You also do not need to create intermediates DataFrames to hold the idxmax/max (I still showed a simpler method to compute it).
Here is a simple version to plot your curves and the greatest extremum. I also added the same color to match the lines.
JavaScript
1
22
22
1
import pandas as pd
2
from matplotlib import pyplot as plt
3
import numpy as np
4
5
np.random.seed(0)
6
df0 = pd.DataFrame(np.random.randint(0,1000,size=(18, 4)), columns=list('ABCD'))
7
#df0 = pd.read_excel("est.xlsx")
8
ax = df0.plot()
9
10
mrkr = ['o', '.', 'v', 'x']
11
mrkr_df = pd.DataFrame(mrkr)
12
13
idx_mx_vals = df0.abs().idxmax().to_list()
14
15
mx_vals = df0.to_numpy()[idx_mx_vals, np.arange(len(idx_mx_vals))].tolist()
16
17
# not needed
18
#df = pd.DataFrame(dict(zip(df, zip(idx_mx_vals, mx_vals, mrkr))))
19
20
for x, y, m, line in zip(idx_mx_vals, mx_vals, mrkr, ax.lines):
21
ax.plot(x, y, marker=m, c=line.get_color(), markersize=20)
22
output: