Last active
March 22, 2020 15:33
-
-
Save ESeufert/508fbf8f96b369bdb2ab0789b79426e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I had an annoying situation where I wanted a multi-chart matplotlib chart to look symmetrical given some number of max | |
# rows and columns | |
# eg. if I have 9 graphs I want to plot and the max number of (rows, columns) for the chart is (4, 4), | |
# I don't want 2 rows with 5 charts on the top and 4 on the bottom; | |
# I want 3 rows with 3 charts per row | |
# this algorithm returns a tuple of rows, columns given some parameters: | |
# this_length is the number of charts to plot | |
# max_cols is the maximum number of columns the chart can have (the matplotlib max is 4) | |
# max_rows is the maximum number of rows the chart can have (matplotlib max is 4) | |
def get_rows_and_cols( this_length, max_cols, max_items ): | |
#returns tuple( rows, cols ) | |
if ( this_length > max_items ): | |
raise Exception( "Max number of graph items is " + str( max_items ) ) | |
return False | |
if is_prime_number( this_length ): | |
if this_length > max_cols: | |
return ( math.ceil( this_length / max_cols ), max_cols ) | |
else: | |
return( 1, this_length ) | |
else: | |
if this_length % max_cols == 0: | |
return( this_length / max_cols, max_cols ) | |
else: | |
for x in [ j for j in range( 1, this_length ) ]: | |
for z in [ i for i in range( max_cols, 1, -1 ) ]: | |
if z * x == this_length: | |
return ( x, z ) | |
return( ( this_length / max_cols ) + 1, max_cols ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment