matplotlib使日期的轴刻度标签加粗 [英] matplotlib make axis ticks label for dates bold

查看:174
本文介绍了matplotlib使日期的轴刻度标签加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的轴上使用粗体标签,以便我可以使用该图进行发布.我还需要将图例中的行的标签以粗体标出.到目前为止,我可以将轴标签和图例设置为我想要的大小和重量.我还可以将轴标签的尺寸设置为所需的尺寸,但是重量却失败了.

I want to have bold labels on my axis, so I can use the plot for publication. I also need to have the label of the lines in the legend plotted in bold. So far I can set the axis labels and the legend to the size and weight I want. I can also set the size of the axis labels to the size I want, however I am failing with the weight.

这是示例代码:

# plotting libs
from pylab import *
from matplotlib import rc


if __name__=='__main__':

  tmpData = np.random.random( 100 )


  # activate latex text rendering
  rc('text', usetex=True)
  rc('axes', linewidth=2)
  rc('font', weight='bold')

  #create figure
  f = figure(figsize=(10,10))

  ax = gca()

  plot(np.arange(100), tmpData, label=r'\textbf{Line 1}', linewidth=2)

  ylabel(r'\textbf{Y-AXIS}', fontsize=20)
  xlabel(r'\textbf{X-AXIS}', fontsize=20)

  fontsize = 20
  fontweight = 'bold'
  fontproperties = {'family':'sans-serif','sans-serif':['Helvetica'],'weight' : fontweight, 'size' : fontsize}
  ax.set_xticklabels(ax.get_xticks(), fontproperties)
  ax.set_yticklabels(ax.get_yticks(), fontproperties)

  for tick in ax.xaxis.get_major_ticks():
      tick.label1.set_fontsize(fontsize)

  for tick in ax.yaxis.get_major_ticks():
      tick.label1.set_fontsize(fontsize)



  legend()
  show()

  sys.exit()

这就是我得到的:

知道我遗漏了什么或做错了什么以便以粗体显示轴刻度标签吗?

Any idea what I am missing or doing wrong in order to get the axis ticks label in bold?

编辑

我已使用 toms 响应更新了我的代码.但是我现在有另一个问题,因为我需要在x轴上使用 datetime ,这与在普通y轴上的效果不同(很抱歉,不将其放在原始问题中,但我不认为它会改变事情):

I have updated my code using toms response. However I now have another problem, as I need to use datetime on the x-axis, this has not the same effect as on the normal y-axis (sorry for not putting this in in the original question, but I did not think it would change things):

# plotting libs
from pylab import *
from matplotlib import rc, rcParams
import matplotlib.dates as dates

# datetime
import datetime

if __name__=='__main__':

  tmpData = np.random.random( 100 )
  base = datetime.datetime(2000, 1, 1)
  arr = np.array([base + datetime.timedelta(days=i) for i in xrange(100)])


  # activate latex text rendering
  rc('text', usetex=True)
  rc('axes', linewidth=2)
  rc('font', weight='bold')

  rcParams['text.latex.preamble'] = [r'\usepackage{sfmath} \boldmath']

  #create figure
  f = figure(figsize=(10,10))

  ax = gca()

  plot(np.arange(100), tmpData, label=r'\textbf{Line 1}', linewidth=2)

  ylabel(r'\textbf{Y-AXIS}', fontsize=20)
  xlabel(r'\textbf{X-AXIS}', fontsize=20)

  ax.xaxis.set_tick_params(labelsize=20)
  ax.yaxis.set_tick_params(labelsize=20)

  ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%Y'))
  ax.xaxis.set_major_locator(dates.MonthLocator(interval=1))


  legend()

现在我的结果如下:

似乎这些变化不会影响显示或 x 轴刻度标签的权重.

It seems to be that the changes doe not affect the display or rather the weight of the x-axis ticks labels.

推荐答案

我认为问题在于,因为刻度线是在LaTeX数学模式下制作的,所以字体属性不适用.

I think the problem is because the ticks are made in LaTeX math-mode, so the font properties don't apply.

您可以通过使用 rcParams 将正确的命令添加到LaTeX序言中来解决此问题.具体来说,您需要使用\ boldmath来获取正确的格式,并使用\ usepackage {sfmath}来获取sans-serif字体.

You can get around this by adding the correct commands to the LaTeX preamble, using rcParams. Specifcally, you need to use \boldmath to get the correct wieght, and \usepackage{sfmath} to get sans-serif font.

此外,您可以使用 set_tick_params 设置刻度标签的字体大小.

Also, you can use set_tick_params to set the font size of the tick labels.

这里有一些代码可以满足您的需求:

Here's some code that does what you want:

import numpy as np
from matplotlib import rc,rcParams
from pylab import *

tmpData = np.random.random( 100 )


# activate latex text rendering
rc('text', usetex=True)
rc('axes', linewidth=2)
rc('font', weight='bold')
rcParams['text.latex.preamble'] = [r'\usepackage{sfmath} \boldmath']

#create figure
f = figure(figsize=(10,10))

ax = gca()

plot(np.arange(100), tmpData, label=r'\textbf{Line 1}', linewidth=2)

ylabel(r'\textbf{Y-AXIS}', fontsize=20)
xlabel(r'\textbf{X-AXIS}', fontsize=20)

ax.xaxis.set_tick_params(labelsize=20)
ax.yaxis.set_tick_params(labelsize=20)

legend()

这篇关于matplotlib使日期的轴刻度标签加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆