I am learning Python language for about a year now, slowly venturing into the "magical python commands". I have to get my hands dirty to grasp the concept of __self.setattr__
, and this is my first time using it. Please let me know if I am using it "correctly", and in what way, I can improve on it. This is because I do not have any peers to cross-check what I write and therefore I think this channel might be a good one to seek advice.
The following reference is used: Link here
Note that confirmed, recovered, deaths
are DataFrames
.
class PlotGo:
__acceptable_keys_list = ['crd','crd_colors','crd_layout_params', 'Date', 'bar_params']
def __init__(self, **kwargs):
self.fig = go.Figure()
self.markers = {}
for key in kwargs.keys():
if key in self.__acceptable_keys_list:
self.__setattr__(key, kwargs.get(key, None))
def _go_bar(self, *args,**kwargs):
return go.Bar(*args, **kwargs)
def update_graph_crd(self):
for crd_name, df in self.crd.items():
self.fig.add_trace(self._go_bar(x=df[self.__acceptable_keys_list
[self.__acceptable_keys_list.index('Date')]],
y=df[crd_name],
name=crd_name,
marker_color=self.crd_colors[crd_name]))
self.fig.update_layout(**self.crd_layout_params)
self.fig.show()
def reset_fig():
# reset figure to clean state again.
self.fig = go.Figure()
crd_dict = {'crd': {'Confirmed': confirmed, 'Recovered': recovered, 'Deaths': deaths},
'crd_colors': {'Confirmed': 'blue', 'Recovered': 'green', 'Deaths': 'red'},
'crd_layout_params': { 'title':'Worldwide Corona Virus Cases - Confirmed, Deaths, Recovered (Bar Chart)',
'xaxis_tickfont_size':12,
'yaxis':dict(title='Number of Cases',
titlefont_size=16,
tickfont_size=12,)}}
a = PlotGo(**crd_dict)
a.update_graph_crd()