Home > Mobile >  how to assign different markers to the max value found in each column in the plot
how to assign different markers to the max value found in each column in the plot

Time:05-06

How would I assign markers of different symbols to each of the max values found in each curve?, Ie, 4 different markers showing the max value in each curve.

Here is my attempt

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


df = pd.DataFrame(np.random.randint(0,1000,size=(100, 4)), columns=list('ABCD'))
maxValues=df.max()
m=['o', '.', ',', 'x',]
df.plot()
plt.plot(maxValues, marker=m)

In my real df, the number of columns will vary.

CodePudding user response:

You can do it this way. Note that I used a V instead of , as the comma (pixel) wasn't showing up clearly.

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

df = pd.DataFrame(np.random.randint(0,1000,size=(100, 4)), columns=list('ABCD'))

df.plot(figsize=(20,5))
mrk = pd.DataFrame({'A': [df[['A']].idxmax()[0], df['A'].max(), 'o'],
                   'B': [df[['B']].idxmax()[0], df['B'].max(), '.'],
                   'C': [df[['C']].idxmax()[0], df['C'].max(), 'v'],
                   'D': [df[['D']].idxmax()[0], df['D'].max(), 'x']})
for col in range(len(mrk.columns)):
    plt.plot(mrk.iloc[0,col], mrk.iloc[1, col], marker=mrk.iloc[2, col], markersize=20)

I created the mrk dataframe manually as it was small, but you can use loops to go through the various columns in your real data. The graph looks like this. Adjust markersize to increase/decrease size of the markers.

Output graph

  • Related