1

I want to make the legend 'bold' I also want to change the font style of the legend like times new roman etc.

plt.gca().legend(('Experimental  Values','Simulated Values'))
params = {'legend.fontsize': 15, 'legend.handlelength': 1}
plot.rcParams.update(params)
plt.figure(figsize=(6,6))
傅能杰
  • 105
  • 1
  • 11
  • 1
    sir, I didn't get you completely but I think you are not happy with my question I guess. I am not able to find the command which can suit in my code. like I also try legend.fontfamily, legend.weight etc. but they didn't work. Please help me. – 傅能杰 Apr 04 '19 at 08:14
  • 1
    roll back your edit because that was working code. I meant that you have to show effort. Matplotlib and python have many possibilities for getting things bold... you need to read tutorials to get there and when you try and hit an error then we can help you. It's all about showing the effort and thus a glimp of your learning curve so others can learn too.. Not the outsourcing skills.. Worst case... learn pandas. – ZF007 Apr 04 '19 at 08:17
  • 1
    I got the answer finally. The following command works for me and followed the matplotlib document for this. There I got the font.weight: bold command. Thank you @ ZF007, I follow your advice and try to read tutorials very carefully and end up finding the solution. https://matplotlib.org/api/matplotlib_configuration_api.html .....params = {'legend.fontsize':10, 'legend.handlelength': 1, 'font.weight': 'bold'} – 傅能杰 Apr 07 '19 at 11:26

2 Answers2

3

edit/update: I have found a nice and complete example here

Try this:

import numpy as np
import matplotlib.font_manager as font_manager


fig, (ax0) = plt.subplots(1, 1, figsize=(10,5));

xx = np.arange(10)
yy = 0.3*xx

plt.plot(xx,yy,label='Line')
font = font_manager.FontProperties(family= 'Comic Sans MS',  # 'Times new roman', 
                                   weight='bold',
                                   style='normal', size=25)
plt.legend(loc="best" , prop=font)
plt.show()

enter image description here

pyano
  • 1,885
  • 10
  • 28
  • 1
    thank you so much sir. actually sir I saw this code earlier. but what I want is that in my code I am not sure is there any method by which I could make the legend bold? In the following command I tried legend.weight: 'bold' but it didn't work. params = {'legend.fontsize': 15, 'legend.handlelength': 1} – 傅能杰 Apr 07 '19 at 09:55
  • 1
    params = {'legend.fontsize':10, 'legend.handlelength': 1, 'font.weight': 'bold'} is finally working. @pyano thank you so much for responding. – 傅能杰 Apr 07 '19 at 11:28
  • Fine, if you have been successful ! I wonder why "my" code works for me but does not works for you. Did you `import matplotlib.font_manager as font_manager` ? – pyano Apr 08 '19 at 11:57
  • Sir, I tried your code and it also worked. But I want to do it a different way. – 傅能杰 Apr 08 '19 at 13:20
  • 1
    Cool :-). And since you are new here: If you find some useful answers her in SO then you can give an upvote (orange arrow). If you have asked a question and have got an answer, that solves your problem you might click "solved" (= check sign below the up/down arrows, which becomes green) too. – pyano Apr 08 '19 at 18:48
1

The following code solved my problem.

plt.gca().legend(('Experimental  Values','Simulated Values'))
params = {'legend.fontsize':10, 'legend.handlelength': 1, 'font.weight': 'bold'} 
plot.rcParams.update(params)
plt.figure(figsize=(6,6))
傅能杰
  • 105
  • 1
  • 11