Reputation: 187
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+
Upvotes: 1
Views: 145
Reputation: 4045
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 )
Upvotes: 1