Question
I want to make some modifications to a few selected tick labels in a plot. For example, if I do:
- label = axes.yaxis.get_major_ticks()[2].label
- label.set_fontsize(size)
- label.set_rotation('vertical')
- label.set_text('Foo')
- print label.get_text()
How-To
Caveat: Unless the ticklabels are already set to a string (as is usually the case in e.g. a boxplot), this will not work with any version of matplotlib newer than 1.1.0. If you're working from the current github master, this won't work. I'm not sure what the problem is yet... It may be an unintended change, or it may not be...
Normally, you'd do something along these lines:
- """
- Simple demo of a scatter plot.
- """
- import numpy as np
- import matplotlib.pyplot as plt
- fig, ax = plt.subplots()
- N = 50
- x = np.random.rand(N)
- y = np.random.rand(N)
- colors = np.random.rand(N)
- area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
- plt.scatter(x, y, s=area, c=colors, alpha=0.5)
- # We need to draw the canvas, otherwise the labels won't be positioned and
- # won't have values yet.
- fig.canvas.draw()
- labels = [item.get_text() for item in ax.get_xticklabels()]
- print("xtick labels: {}".format(labels))
- labels[1] = 'Testing' # The second x tick label text is modified to be "Testing"
- ax.set_xticklabels(labels)
- plt.show()
沒有留言:
張貼留言