1

I have a series of data (climbing routes and their grades.)

pd.DataFrame({'route':['Easy route', 'The hard', 'Another route'], 'grade':['9a+', '6c', '7b+']})
route Grade
Easy route 9a+
The Hard 6c
Another route 7b+

Grades are strings (non numerical), but can be easily converted to numerical data. Example:

def convert(grade):
    grades = ['6b+', '6c', '6c+', '7a', '7a+', '7b', '7b+', '7c', '7c+', '8a', '8a+', '8b', '8b+', '8c', '8c+', '9a', '9a+']
    return grades.index(grade)

Table like such:

pd.DataFrame({'route':['Easy route', 'The hard', 'Another route'], 'grade':[16, 6, 1]})
       route  grade
0     Easy route     16
1       The hard      6
2  Another route      1

Can be plotted with ease. What I want is a plot with the original grades, like this. What is the workaround for non numerical data?

   Easy route   *************************************
     The hard   ************
Another route   *****************************
                6a 6a+ 6b 6c 6c+ 7a 7a+ 7b 7b+ ... 9a+
1
  • Maybe plt.bar(x=df.route, height=df.grade)? Commented Feb 7, 2021 at 23:05

1 Answer 1

1

I suggest to first add a numeric representation of the grades in order to maintain the order when plotting. Afterwards, you can set the names of the grades manually as yticklabels:

from matplotlib import pyplot as plt
import pandas as pd
# to example
df = pd.DataFrame({'route':['Easy route', 'The hard', 'Another route'], 'grade':['9a+', '6c', '7b+']})

# original grades
grades = ['6b+', '6c', '6c+', '7a', '7a+', '7b', '7b+', '7c', '7c+', '8a', '8a+', '8b', '8b+', '8c', '8c+', '9a', '9a+']
# define function
def convert(grade,grades):
    return grades.index(grade)
# add numeric grades
df['grade_num'] = [convert(g,grades) for g in df['grade']]

# open figure
fig, ax = plt.subplots()
# ensure limits
ax.set_ylim([0,len(grades)])
ax.bar( x=df['route'], height=df['grade_num'] )
# ensure limits
ax.set_ylim([0,len(grades)])
# set ticks
ax.set_yticks( list(range(len(grades))) )
# set tick labels
ax.set_yticklabels( grades )

resulting plot

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

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.