0

I want to generate parameters for figures dynamically, pass as arguments to Matplotlib.

Example to illustrate, this works:

dict = {'x_tick_fontsize':12 ,'x_tick_weight':'normal','x_tick_rotation':15}
            
plt.xticks(fontsize = dict['x_tick_fontsize']  
           ,weight = dict['x_tick_weight'] 
           ,rotation = dict['x_tick_rotation']) 

This doesn't, but I want it to:

my_params_string = "fontsize = 12, weight ='normal', rotation = 15"
plt.xticks(my_params_string)

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Programming_Learner_DK
  • 1,509
  • 4
  • 23
  • 49
  • 1
    First, don't use `dict` as a variable name! Second, `plt.xticks(**my_dict)` should work fine. https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – Jody Klymak Jun 27 '23 at 05:59
  • @Jody, why do you say don't use `dict`? It's redundant? – Programming_Learner_DK Jun 27 '23 at 06:25
  • 1
    `dict` is a built-in function to construct a dictionary. By assigning it to a variable, you can't use the built-in anymore. Same goes with `list`, `set`, `tuple`, `sum`, `iter` etc. – paime Jun 27 '23 at 09:11
  • @JodyKlymak if you post as an answer I will accept - it worked. Thx! – Programming_Learner_DK Jun 27 '23 at 09:40
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Jul 01 '23 at 19:13
  • @halfer I will be a better question asker/member moving forward. – Programming_Learner_DK Jul 02 '23 at 12:12
  • Thank you! Useful meta references: [Confident asking](https://meta.stackoverflow.com/questions/366264/how-can-we-encourage-new-authors-to-ask-confident-questions) and [Removing fluff](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-like-greetings-signatures-thanks-etc-when-editin). – halfer Jul 02 '23 at 13:45

0 Answers0