How to the y-axis label qualitative values (words) instead of quantitative values (numbers) in a matplotlib python?
Question:
I am trying to plot a matplotlib graph that takes an x value (quantitative) as input and outputs a qualitative y value (depicted on the y-axis of my graph). For example, say x = 1,2,3,4,5,6,7,8,9,10. For x = 1,2,3 -> y should = low. For x = 4,5,6 -> y should = medium, and for x = 7,8,9,10 -> y should = high. I want this to be done using matplotlib in python.
I found the example below, but it is hard to follow. Could someone explain it or refer me to a more understandable example? Thank you.
Answers:
We can slice index x
(i.e. x[1::3]
which is x
at indexes 1
, 4
, 9
) when setting the xticks
and then we can provide xticklabels
a list
of labels (e.g. ['low'
, 'medium'
, 'high'
]):
import matplotlib.pyplot as plt
import numpy as np
x = range(10)
y = np.random.random(10)
fig = plt.figure(figsize=(5, 5))
ax = plt.subplot(1, 1, 1)
ax.plot(x, y, ls='-', lw=1.5, color=[1, 0, 0])
ax.set_xticks(x[1::3])
ax.set_xticklabels(['low', 'medium', 'high'])
plt.show()
I am trying to plot a matplotlib graph that takes an x value (quantitative) as input and outputs a qualitative y value (depicted on the y-axis of my graph). For example, say x = 1,2,3,4,5,6,7,8,9,10. For x = 1,2,3 -> y should = low. For x = 4,5,6 -> y should = medium, and for x = 7,8,9,10 -> y should = high. I want this to be done using matplotlib in python.
I found the example below, but it is hard to follow. Could someone explain it or refer me to a more understandable example? Thank you.
We can slice index x
(i.e. x[1::3]
which is x
at indexes 1
, 4
, 9
) when setting the xticks
and then we can provide xticklabels
a list
of labels (e.g. ['low'
, 'medium'
, 'high'
]):
import matplotlib.pyplot as plt
import numpy as np
x = range(10)
y = np.random.random(10)
fig = plt.figure(figsize=(5, 5))
ax = plt.subplot(1, 1, 1)
ax.plot(x, y, ls='-', lw=1.5, color=[1, 0, 0])
ax.set_xticks(x[1::3])
ax.set_xticklabels(['low', 'medium', 'high'])
plt.show()