Python matplotlib 使日期的轴刻度标签加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29766827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib make axis ticks label for dates bold
提问by red_tiger
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.
我想在我的轴上使用粗体标签,以便我可以使用该图进行发布。我还需要将图例中线条的标签以粗体绘制。到目前为止,我可以将轴标签和图例设置为我想要的大小和重量。我还可以将轴标签的大小设置为我想要的大小,但是我的重量失败了。
Here is an example code:
这是一个示例代码:
# 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()
And this is what I get:
这就是我得到的:
Any idea what I am missing or doing wrong in order to get the axis ticks label in bold?
知道我遗漏了什么或做错了什么以便以粗体显示轴刻度标签吗?
EDIT
编辑
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):
我已经使用 toms 响应更新了我的代码。但是我现在有另一个问题,因为我需要datetime
在 x 轴上使用,这与正常 y 轴上的效果不同(抱歉没有把它放在原始问题中,但我认为它不会改变事物):
# 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()
Now my result looks like this:
现在我的结果是这样的:
It seems to be that the changes doe not affect the display or rather the weight of the x-axis ticks labels.
似乎这些变化不会影响显示或 x 轴刻度标签的权重。
采纳答案by tmdavison
I think the problem is because the ticks are made in LaTeX math-mode, so the font properties don't apply.
我认为问题是因为刻度是在 LaTeX 数学模式下制作的,所以字体属性不适用。
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.
您可以通过使用rcParams
. 具体来说,您需要使用 \boldmath 来获得正确的 wiegt,并使用 \usepackage{sfmath} 来获得 sans-serif 字体。
Also, you can use set_tick_params
to set the font size of the tick labels.
此外,您可以使用set_tick_params
来设置刻度标签的字体大小。
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()