CovidChart Class#
- class CovidChart.CovidChart#
Bases:
object
This class is used to create charts using COVID-19 data. All formatting is carried out by this class with lines of best fit added for scatter graphs. To change graph formatting amend the method draw_chart()
- add_bar_chart(x_data, y_data, colour, label='', display_vals=True, to_bar_over=True)#
Use when creating just bar charts without a line of best fit and totals at the top of each bar
- Args:
- x_data
List or DataFrame, data for the x-axis of your bar plot
- y_data
List or DataFrame, data for the y-axis of your bar plot
- colour
String Value, color of the plot, can be a word such as “red” or a hex value
- label
String Value optional, This is the label used for the legend, to_bar_over must be set to False for the legend to be displayed
- display_vals
Boolean Value optional, set to true to display numbers above the bars
- to_bar_over
Boolean Value optional, set to true if you don’t want a legend, set to false for a legend
- add_bar_plot(x_data, y_data, colour, label)#
Adds a bar plot with line of best fit to a plot, this just adds the data once draw_chart is called the plot will be saved and/or displayed on screen
- Args:
- x_data
List or DataFrame, data for the x-axis of your bar plot
- y_data
List or DataFrame, data for the y-axis of your bar plot
- colour
String Value, color of the plot, can be a word such as “red” or a hex value
- label
String Value, The label for the plot, this will be used in the legend
- add_scatter_plot(x_data, y_data, colour, label, to_dash, data_complete, to_clip=False, tolerance=1.2)#
Adds a scatter plot with line of best fit to a plot, this just adds the data once draw_chart is called the plot will be saved and/or displayed on screen
- Args:
- x_data
List or DataFrame, data for the x-axis of your scatter chart
- y_data
List or DataFrame, data for the y-axis of your chart
- colour
String Value, color of the plot, can be a word such as “red” or a hex value
- label
String Value, The label for the plot, this will be used in the legend
- to_dash
Boolean Value, decides if the plot should have a solid or dashed line of best fit
- data_complete
Boolean value, if set to true the LOBF will go to the end, if set to False the LOBF will go to (values - (time/2)), set to False if data is incomplete
- to_clip
Boolean Value, if this is true any plot that is greater than the (averaged value * tolerance) then the plot value will be changed to the average value, this allows us to clip data points that are alot higher than the averaged value.
- tolerance
Float Value, the tolerance for clipping, set to 1.2 by default which is 20%.
- add_treemap(data, labels, colours)#
Creates a treeMap diagram with data (list) and labels (list) The data should already be summed when calling this method therefore data should be an list of summed data
- Args:
- data
List or DataFrame, data for the treemap. This data should not be timeseries, see the example in example.py
- labels
List or DataFrame, The label for the plot, this will be used in the legend
- colours
List or DataFrame, color of the plot, can be a word such as “red” or a hex value
- clear_chart()#
Clears the chart saving the original params
..Note:: This should be called before creating another chart
- create_time_stamp(img_path, x_pos, y_pos, fontsize, to_be_wide, hos_data=False)#
Adds a timestamp to the chart with the website URL
- Args:
- img_path
String Value, image path of where the chart is saved
- x_pos
Integer Value, start position of the timestamp for the x-axis
- y_pos
Integer Value, start position of the timestamp for the y-axis
- fontsize
Integer Value, size of the fonts to be used
- to_be_wide
Boolean value, is the charts aspect ratio wide or portait - decides if the stamp is to go at the top or bottom of the chart
Note
image_path is the path to the image including the file name extension, this will be .png for charts created by this class
- draw_Scatter_Year_Comp(data, to_show, label, to_be_wide, y_dates, to_stamp)#
Give this method some data and it will split it into years and plot a year comparison with daily averages
- Args:
- data
List, data be to plotted on the y-axis
- to_show
Boolean Value, deicdes if the chart should be shown
- label
String Value, sets the label for the plots also used in the file name and title
- to_be_wide
Boolean Value, sets aspect ratio to either landscape or portrait
- y_dates
List, list of dates for the y-axis by default I used data/static/dates.csv, this will give you comparison charts starting and ending at March of each year
- to_stamp
Boolean Value, decides if a time stamp should be added to the chart
- draw_chart(x_axis_title, y_axis_title, title, file_name, to_be_wide, hos_data=False)#
Draws the chart either on screen if to_show == true (set_chart_params) or as a png image saved with the file_name variable These graphs use ONS guidelines for formatting
- Args:
- x_axis_title
String Value, Title for the x-axis
- y_axis_title
String Value, Title for the y-axis
- title
String Value, Title for the chart
- file_name
String Value, file name of the chart (DO NOT INCLUDE FILE EXTENSION; this is added by this method)
- to_be_wide
Boolean value, decides the aspect ratio of the chart, wide or portrait
Note
Do not add the file extension for the file name argument, this extension will always be .png and is added by this method
- draw_v_lines()#
Draws vertical lines on charts indicating key moments. If any key moments need to be added add them in this method get_gov_date_Series() or govAgedDateSeries() should be used with this method and not dataframe[“date”] as this will give an error
- reset_max_y_value()#
Resets ylimit if it was explicitly set
- set_LOBF_time(time)#
Changes the averaged time in days for LOBF set my the argument time
- Args:
- time
This is the amount of time in days for averaging the data
- set_chart_params(to_show, show_leg, to_add, to_stamp)#
Use to change the parameters of the COVID chart i.e. to show the chart, to add a time stamp, etc
- Args:
- to_show
Boolean Value, decides whether to show the plot in python
- show_leg
Boolean Value, decides if the default COVID legend should be displayed with VLINES. You might want to show the VLINES without the legend
- to_add
Boolean Value, Boolean Value, decides if the VLINES should be added to the chart
- to_stamp
Boolean Value, decides if the time stamp should be added to the chart
..Note:
- set_legend_bottom(value)#
Use this if you wnat to change the position of the legend
- Args:
- value
Boolean Value, if set to True the legend will be at the bottom of the chart otherwise it will be at the top left of the chart
- set_legend_cols(value)#
Use this to change how many columns we have in the legend
- Args:
- value
Integer Value, this will be the number of columns in the legend when its at the botton of the chart
- set_max_y_value(value)#
sets the maximum Y valve
- Args:
- value
Integer Value, sets the maxmum value for the Y-axis
- set_start_date(start_date)#
Sets the start date for the graph to be used if you want to show VLINES when starting from a different date to the default one
- Args:
- start_date
Date Value, used to set a different start date when to_add == True (set_chart_params). By default the start date is 20nd March 2020. This can be changed if required