1

Suppose, I have a data frame like this:

Col1    Col2
 1       A
 5       B
 3       C
 2       D

Specifically in python

DF = pd.DataFrame({'Col1' : [1,5,3,2],'Col2':['A','B','C','D']})

If I plot col1 values in matplotlib with following code:

plt.plot(DF.Col1)

I get this figure enter image description here

Now I want to annotate col2, where the values are greater than 2.That's mean, it annotates "B" and "C" at 5 and 3 in the figure. How can I do that?

2 Answers 2

1

Col1 contains the Col2 letters values, so you can read every value of col1 and check if is greather than 2. If it's so, you can take the letter at the same index in Col2.

Here is an example:

d={'Col1' : [1,5,3,2],'Col2':['A','B','C','D']}

#get the data
col1=d["Col1"]
col2=d["Col2"]

def getGreaterThanNumb(val, lett, numb):
    if len(val) != len(lett):
        #col1 and col2 must have the same lenght!
        return
    for i in range(0, len(lett)):
        if val[i]>numb:
            print(lett[i]) #Print or store it in a collection

getGreaterThanNumb(col1, col2, 2)

Your output will be:

otput screenshot


ANNOTATIONS

Now, the coords of any vertices (based on the previous code) is the couple (x,y) = (i,val[i]), so you can write:

for i in range(0, len(lett)):
        if val[i]>numb:
            #print(lett[i]) #Print or store it in a collection
            plt.annotate(lett[i], (i,val[i])) #Annotate

Here is a complete code (without using Pandas, but the behaviour is the same):

import matplotlib.pyplot as plt
d={'Col1' : [1,5,3,2],'Col2':['A','B','C','D']}
#get the data
col1=d["Col1"]
col2=d["Col2"]

coords = plt.plot(col1)
plt.ylabel('some numbers')


def getGreaterThanNumb(val, lett, numb):
    if len(val) != len(lett):
        #col1 and col2 must have the same lenght!
        return
    for i in range(0, len(lett)):
        if val[i]>numb:
            plt.annotate(lett[i], (i,val[i])) #Annotate

#Call the annotation method
getGreaterThanNumb(col1, col2, 2)

#plot
plt.show()

Your output will be:

graph

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, I know how to get the element which are greater than 2. I am not expert of matplotlib. I want to plot those in the graph. I am searching a way of ploting those points
1

Please Check this snippet .I have created another dataframe named x which holds your sorted value based on requirement.

Here to show the difference between both conditions, I created 2 line plots that overlaps based on your value. But if you just want single line then you can remove

ax.plot(x['Col1'],label='Line1')

plot

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'Col1' : [1,5,3,2],'Col2':['A','B','C','D']})
x=df.loc[df['Col1'] > 2]
fig, ax = plt.subplots()
ax.plot(df['Col1'],label='Line2')
ax.plot(x['Col1'],label='Line1')
for x,y,z in zip(x.index.tolist(),x['Col1'],x['Col2']):
  ax.annotate(z,xy=(x,y))
plt.legend()
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.