Так что мне удается изменить шрифт отметок x и y (см. Рисунок 2) при использовании линейной шкалы на обеих осях. Но как только я использую логарифмическую шкалу на обеих осях, это больше не работает.
У меня два вопроса:
- В случае линейного масштаба (рисунок 2), есть ли способ оптимизировать код? Потому что у меня есть 6 строк только для того, чтобы изменить шрифт для галочек. Есть ли «волшебная» строка, которую я могу добавить в начале своего кода, чтобы сделать его автоматическим?
- Почему я получаю эту ошибку в логарифмическом случае (чтобы создать рисунок 3)?
Спасибо!
Тити
Рисунок 1
Рисунок 2
import matplotlib.pyplot as plt
from numpy import *
from matplotlib import *
rcParams['mathtext.fontset'] = 'cm'
rcParams['mathtext.rm'] = 'cm'
# define datas
x = linspace(1,100,100)
####### FIGURE 1 #######
fig, axs = plt.subplots(nrows=1, ncols=1)
axs.plot(x, x)
axs.set_xscale('linear')
axs.set_xscale('linear')
plt.savefig('fig1.pdf')
####### FIGURE 2 #######
fig, axs = plt.subplots(nrows=1, ncols=1)
axs.plot(x, x)
axs.set_xscale('linear')
axs.set_xscale('linear')
if 1:
fig.canvas.draw()
# change font of xticks label
labels = [item.get_text() for item in axs.get_xticklabels()]
labels = ['$%s$' % (labels[j]) for j in range(len(labels))]
axs.set_xticklabels(labels)
# change font of yticks label
labels = [item.get_text() for item in axs.get_yticklabels()]
labels = ['$%s$' % (labels[j]) for j in range(len(labels))]
axs.set_yticklabels(labels)
plt.savefig('fig2.pdf')
####### FIGURE 3 #######
fig, axs = plt.subplots(nrows=1, ncols=1)
axs.plot(x, x)
axs.set_xscale('log')
axs.set_yscale('log')
if 1:
fig.canvas.draw()
# change font of xticks label
labels = [item.get_text() for item in axs.get_xticklabels()]
labels = ['$%s$' % (labels[j]) for j in range(len(labels))]
axs.set_xticklabels(labels)
# change font of yticks label
labels = [item.get_text() for item in axs.get_yticklabels()]
labels = ['$%s$' % (labels[j]) for j in range(len(labels))]
axs.set_yticklabels(labels)
plt.savefig('fig3.pdf')
Traceback (most recent call last):
File "test.py", line 50, in <module>
plt.savefig('fig3.pdf')
File "/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 710, in savefig
res = fig.savefig(*args, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 2035, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2263, in print_figure
**kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2586, in print_pdf
self.figure.draw(renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/axis.py", line 1192, in draw
renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/text.py", line 922, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/text.py", line 309, in _get_layout
ismath=ismath)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2153, in get_text_width_height_descent
self.mathtext_parser.parse(s, 72, prop)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/mathtext.py", line 3293, in parse
box = self._parser.parse(s, font_output, fontsize, dpi)
File "/anaconda2/lib/python2.7/site-packages/matplotlib/mathtext.py", line 2521, in parse
six.text_type(err)]))
ValueError:
$$\mathdefault{10^{0}}$$
^
Expected end of text (at char 0), (line:1, col:1)