Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the Charts class in rick.py #7

Open
5 tasks
MelinaGoula opened this issue Nov 30, 2023 · 0 comments
Open
5 tasks

Refactoring the Charts class in rick.py #7

MelinaGoula opened this issue Nov 30, 2023 · 0 comments

Comments

@MelinaGoula
Copy link
Contributor

Context

The charts class in rick.py should be reformatted for better readability and ability to be generalized.

Potential improvements are:

  • Replacing repeated code blocks with sub functions
  • Updating functions to be able to support different input sizes, specifically, any bar or line charts should be able to handle more than just 1 or 2 columns of data
    • For example, bar_chart only supports graphing 2 columns of data but this could be extended to any number of columns
  • Changing some class and function names for clarity

Outline of new structure

Plotting functions currently follow a relatively similar structure which could be replaced with the following functions (and potentially more):

calculate_y_params() # calculates the minimum, maximum and increment values for the y axis 
set_plot_style() # sets the grid, background color and size of the plot 
set_ticks() # sets the x and y ticks
set_labels() # sets x and y axis labels
set_annotations() # adds any additional annotations to the plot 
set_legend() # adds a legend to the plot 

Note: function arguments are in progress

A general plotting function would then follow the format below.

def example_plotting_function(df, ylab, xlab, **kwargs):
    '''
    Description of function including inputs, outputs and any kwargs. 
    '''
    func() # Should be renamed to be more clear 
    y_min, y_max, y_inc = calculate_y_params()
    fig, ax = example_plot() # depends on each function 
    fig, ax = set_plot_style() 
    fig, ax = set_ticks()
    fig, ax = set_labels()
    fig, ax = set_annotations()
    fig, ax = set_legend()
    return fig, ax

def example_plot(): 
	'''Description of function'''
	for i, col in data.columns: 
		## plot of specific type e.g. plot(data, ) for lines 

Furthermore, the Charts class can be reorganized to use inheritance such that:

  • Charts contains all the functions that are common between the different chart types (the ones listed above) as methods
  • Each chart type (e.g. line, bar chart) is a child class that includes it's own plotting method
class Charts:
	def calculate_y_params():

	def calculate_delta():

	def set_plot_style():

	def set_ticks():

	def set_labels():

	def set_annotations():
	
	def set_legend():

class LineChart(Charts): 
	def plot_line():

class BarChart(Charts): 
	def plot_bars():

To do

  • Capitalize class names to follow CapWords convention
  • Include docstrings / descriptions for all functions
  • Rename the func function to make it more intuitive what it is doing (e.g. init_global_settings or something better)
  • Create a plotting function for each chart type that can be generalized to any number of columns
  • Create / update the methods of the Charts class to ensure that they work for any chart type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant