User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2022/12/12 13:56]
jypeter Improved
other:python:misc_by_jyp [2023/03/28 14:10]
jypeter [matplotlib related stuff] Added the time axes section
Line 254: Line 254:
 Note: a configuration file is also a way to easily store and exchange text data ! Note: a configuration file is also a way to easily store and exchange text data !
  
 +
 +===== Working with global variables =====
 +
 +There is a good chance you don't actually want/need a //global// variable. Be sure to use the ''​global''​ statement correctly if you want to avoid side-effects...
 +
 +  * [[https://​docs.python.org/​3/​faq/​programming.html?​highlight=global#​why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value|Using (and changing) a global variable inside a script or module]]
 +    * Simple module example\\ <​code>​_myvar = 10
 +
 +def set_myvar(new_val):​
 +    # Note: need to explicitly define a global variable (of a module)
 +    # as '​global'​ BEFORE changing its value in a function!
 +    # Otherwise, the value will not be REdefined outside the function
 +    global _myvar
 +    _myvar = new_val
 +
 +def get_myvar():​
 +    return _myvar
 +
 +def myfunc(nb_repeat = 10):
 +    print(nb_repeat * _myvar)</​code>​
 +  * [[https://​docs.python.org/​3/​faq/​programming.html?​highlight=global#​how-do-i-share-global-variables-across-modules|Sharing global variables across modules]]
 ===== Sorting ===== ===== Sorting =====
  
Line 432: Line 453:
 >>>​ np.add.reduceat(np.sort(vals),​ slices_indices) >>>​ np.add.reduceat(np.sort(vals),​ slices_indices)
 array([3. , 4.5, 8. ])</​code>​ array([3. , 4.5, 8. ])</​code>​
 +
 +
 +===== matplotlib related stuff =====
 +
 +==== Working with time axes (and ticks) ====
 +
 +If you have problems setting the limits of a time axis, choosing the ticks' locations, or specifying the style of the labels, you should check the:
 +  * [[https://​matplotlib.org/​stable/​gallery/​index.html#​ticks|Ticks examples'​ gallery]]
 +  * [[https://​matplotlib.org/​stable/​gallery/​text_labels_and_annotations/​date.html|Date tick labels example]]
  
  
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter