def print_heatmap(): today = pytz.utc.localize(datetime.datetime.now()) min_date = today days = [] values = [] for workout in workouts: days.append( workout.date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)) values.append(workout.duration_minutes()) if workout.date < min_date: min_date = workout.date days = np.asarray(days) events = pd.Series(values, index=days) calmap.yearplot(events, year=2018) # all_days = pd.date_range('1/15/2014', periods=700, freq='D') # days = np.random.choice(all_days, 5) # events = pd.Series(np.random.randn(len(days)), index=days) # calmap.calendarplot(events, monthticks=3, daylabels='MTWTFSS', # dayticks=[0, 2, 4, 6], cmap='YlGn', # fillcolor='grey', linewidth=0, # fig_kws=dict(figsize=(8, 4))) plt.pyplot.show()
def calendar_plot_by_year(series_to_plot, normalize_each_year=False): """Creates a calendar heatmap of of data contained in a Series Arg: series_to_plot: Pandas Series object [index = dates] normalize_each_year: Boolean flag to separately color each year """ # calculate number of years start_year = series_to_plot.sort_index().index[0].year end_year = series_to_plot.sort_index().index[-1].year num_years = end_year - start_year + 1 # send directly to function if we want to few heatmap across full time period if not normalize_each_year: calmap.calendarplot(series_to_plot, cmap='YlGn', fillcolor='grey', \ linewidth=.05, daylabels=['M', 'T', 'W', 'T', 'F', 'S', 'S'], \ fig_kws=dict(figsize=(12, 2*num_years))) return # plot each year and build figure fig = plt.figure(figsize=(12, 2 * num_years)) for i, year in enumerate(range(start_year, end_year + 1), 1): year_to_plot = series_to_plot[series_to_plot.index.year == year] # plot axes = fig.add_subplot(num_years, 1, i) calmap.yearplot(year_to_plot, year=year, cmap='YlGn', fillcolor='grey', linewidth=.05, \ daylabels=['M', 'T', 'W', 'T', 'F', 'S', 'S'], ax=axes) axes.set_ylabel(year)
def graph_vists(): """ Graphs the frequency of page visits. """ vists = load_file("vists") keys = [day for day in vists["/"]] days = pd.to_datetime(keys) calmap.yearplot(pd.Series([vists["/"][k] for k in keys], index=days), year=get_year()) plt.savefig("static/img/heatmap.png", bbox_inches="tight") #pad_inches=0)
def plot_calendar(df, habitname, show=True, year=None): df = df[df.index.get_level_values("HabitName").isin([habitname])].reset_index() df = df.set_index(pd.DatetimeIndex(df["CalendarDate"])) if year: calmap.yearplot(df["Value"], year=year) else: calmap.calendarplot(df["Value"]) if show: plt.show()
def getHeatMap(self): print('Getting heat map...') c = Counter() for song in self.songs: t = pd.to_datetime( song.start_time).tz_localize('UTC').tz_convert('Asia/Shanghai') c[pd.to_datetime(t.date())] += 1 events = pd.Series(list(c.values()), index=list(c.keys())) calmap.yearplot(events, year=int(self.year)) plt.savefig('HeatMap{}.png'.format(self.year), dpi=300)
def win_loss_year_plot(prizes_df): from matplotlib.patches import Patch fig, axes = plt.subplots(nrows=5, ncols=1, squeeze=False, figsize=(10, 12.5), subplot_kw={}, gridspec_kw={}) axes = axes.T[0] for year_idx, year in enumerate([2016, 2017, 2018, 2019, 2020]): ax = axes[year_idx] year_df = filter_year(prizes_df, year) non_repeated_df = filter_non_repeated(year_df) win_pct = (non_repeated_df['win'] == 1).sum() / len(non_repeated_df) loss_pct = (non_repeated_df['win'] == 0).sum() / len(non_repeated_df) results_df = year_df['win'].resample('D').asfreq(fill_value=np.nan) text_df = year_df['repeated'].resample('D').asfreq(fill_value='') text_df[text_df == 0] = '' text_df[text_df == 1] = 'R' calmap.yearplot(results_df, year=year, daylabels=PT_DAY_LABELS, monthlabels=PT_MONTH_LABELS, how=None, ax=ax, vmin=0, vmax=1, cmap=ListedColormap(['red', 'green']), text=text_df) ax.set_title( f'Montras ganhas e perdidas no Preço Certo em Euros ({year})') leg = ax.legend( handles=[ Patch(facecolor='r', edgecolor='r', label=f'Montra perdida {loss_pct:.1%}'), Patch(facecolor='g', edgecolor='g', label=f'Montra ganha {win_pct:.1%}'), ], loc='lower center', ncol=4, bbox_to_anchor=(0.5, -.65), title='Probabilidades (ignorando episódios repetidos R)') leg._legend_box.align = 'left' plt.tight_layout() fig.savefig('./year_win_loss.png')
def plot(self, date): try: data = self.evaluator._get_flat_data_until(date) all_days = pd.date_range("1/1/2020", periods=len(data), freq="D") events = pd.Series(data, index=all_days) ax = plt.subplot(111) calmap.yearplot(events, year=2020, ax=ax) plt.title(f"Deep Work Calendar for {self.evaluator.name}") plt.show() except Exception as ex: print(ex) with np.printoptions(precision=3, suppress=True): print(data)
def core_con(): temp = full_table.groupby('Date')['Confirmed'].sum() tempp = temp.diff() plt.figure(figsize=(20, 5)) ax = calmap.yearplot(tempp, fillcolor='white', cmap='Reds', linewidth=0.5) print(ax)
def test_yearplot_cmap_fillcolor_linewidth(events): """ The appearance can be changed by using another colormap. Here we also use a darker fill color for days without data and remove the lines. """ ax = calmap.yearplot(events, cmap='YlGn', fillcolor='grey', linewidth=0) return ax.figure
def plot_calmap(df_serie, title='Calendar 2018'): """ Function that plots a Calendar Map Arguments: df_serie {Pandas series} -- Pandas series with the index in a day frequency: such as: 2018-01-01 4959 2018-01-02 55614 2018-01-03 60449 ... ... title {string} -- Title of the figure """ plt.style.use('seaborn') fig = plt.figure(figsize=(15, 6)) ax = fig.add_subplot(111) cax = calmap.yearplot(df_serie, ax=ax) #, cmap='YlGn') cb = fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal') plt.title(title) #increase all text ax = plt.gca() for item in ([ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(16) cb.ax.tick_params(labelsize=14) ax.title.set_fontsize(22) return fig
def test_yearplot_monthticks_daylabels_dayticks(events): """ We can ask to draw only every nth label, or explicitely supply the label indices. The labels themselves can also be customized. """ ax = calmap.yearplot(events, monthticks=3, daylabels='MTWTFSS', dayticks=[0, 2, 4, 6]) return ax.figure
def test_yearplot_monthticks_daylabels_dayticks(events): """ We can ask to draw only every nth label, or explicitely supply the label indices. The labels themselves can also be customized. """ ax = calmap.yearplot( events, monthticks=3, daylabels="MTWTFSS", dayticks=[0, 2, 4, 6] ) return ax.figure
def plot_year_heatmap(data, width=800, height=200, style='whitegrid', format=SVG, cache=True): ax = calmap.yearplot(data, how=None, dayticks=(0, 2, 4, 6), cmap='YlGn', fillcolor='#eeeeee') return ax.get_figure(), width, height
def heatmap_data(data1, data2, data3): value1 = [] value2 = [] value3 = [] all_day = pd.date_range('01/01/2018', periods=365, freq='D') for i in range(365): value1.append(data1["Value"][i]) value2.append(data2["Value"][i]) value3.append(data3["Value"][i]) events1 = pd.Series(value1, index=all_day) events2 = pd.Series(value2, index=all_day) events3 = pd.Series(value3, index=all_day) fig1 = plt.figure(figsize=(20, 8)) #fig2 = plt.figure(figsize=(20,8)) #fig3 = plt.figure(figsize=(20,8)) #plt.subplot(3, 1, 1) #cax1 = calmap.yearplot(events1, year = 2018) #fig1.suptitle('2018 Chaiyi Chaiyi', fontsize = 20, y = 0.65) #plt.subplot(3, 1, 2) #cax2 = calmap.yearplot(events2, year = 2018) #fig1.suptitle('2018 Yulin Mailiao', fontsize = 20, y = 0.05) #plt.subplot(3, 1, 3) cax3 = calmap.yearplot(events3, year=2018) #fig1.colorbar(cax1.get_children()[1], cax=cax1, ax=cax1) #plt.plot(fig1) #fig2.colorbar(cax1.get_children()[1], cax=cax2, ax=cax2) #plt.plot(fig2) fig1.colorbar(cax3.get_children()[1], cax=cax3, ax=cax3) #plt.plot(fig3) fig1.suptitle('2018 Taitung Taitung', fontsize=20, y=0.85) plt.show()
def daily_cases_calmap(df, title): fig, axs = plt.subplots(3, 1, sharex=True, squeeze=False, figsize=(20, 12)) confirmed_diff = df['Confirmed'].diff() deaths_diff = df['Deaths'].diff() recovered_diff = df['Recovered'].diff() calmap.yearplot(confirmed_diff, fillcolor='white', cmap='Blues', linewidth=0.5, ax=axs[0][0]) calmap.yearplot(deaths_diff, fillcolor='white', cmap='Reds', linewidth=0.5, ax=axs[1][0]) calmap.yearplot(recovered_diff, fillcolor='white', cmap='BuGn', linewidth=0.5, ax=axs[2][0]) fig.suptitle(f'[Calendar Heatmap] {title}')
time_count = data["Date of Incident (month/day/year)"].groupby(data["Date of Incident (month/day/year)"].dt.year).agg('count') time_count=time_count[0:-1] axs[3,0].plot(time_count, color="red") # D/D data day_data = data["Date of Incident (month/day/year)"].groupby(data["Date of Incident (month/day/year)"]).agg('count') day_data.rolling(30,center=True) day_dates = data["Date of Incident (month/day/year)"].value_counts().index day_count = data["Date of Incident (month/day/year)"] print(day_count) axs[0,1].plot(day_data) calmap.yearplot(day_data,daylabels='MTWTFSS',year=2020,ax=axs[3,1]) #day_count.drop() # Drop unknown category #data.drop(data[data["Criminal Charges?"] == "No known charges"].index, inplace = True) # Reframe data for simplification data.loc[data["Criminal Charges?"] == "Charged, Acquitted", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "Charged, Mistrial", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "NO", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "Charged with a crime, Acquitted", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "Charged, Charges Tossed", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "Charged, Charges Dropped", "Criminal Charges?"] = "No" data.loc[data["Criminal Charges?"] == "Charged, Convicted, Sentenced to 30 years in prison", "Criminal Charges?"] = "Charged, Convicted" data.loc[data["Criminal Charges?"] == "Charged, Convicted, Sentenced to 20 years in prison", "Criminal Charges?"] = "Charged, Convicted"
def render_contribution_figure(series): fig = plt.figure(figsize=(8, 2), dpi=72) ax = fig.add_axes([0, 0, 1, 1]) ax = calmap.yearplot(series, ax=ax) return fig
col = int((ind % n_cols) + 1) fig.add_trace(go.Bar(x=temp['Date'], y=temp.loc[temp['Country/Region'] == country, 'Confirmed'], name=country), row=row, col=col) fig.update_layout(height=2000, title_text="No. of new cases in each Country") fig.show() temp = full_table.groupby('Date')['Confirmed'].sum() temp = temp.diff() plt.figure(figsize=(20, 5)) ax = calmap.yearplot(temp, fillcolor='white', cmap='Reds', linewidth=0.5) plt.show() spread = full_table[full_table['Confirmed'] != 0].groupby('Date') spread = spread['Country/Region'].unique().apply(len).diff() plt.figure(figsize=(20, 5)) ax = calmap.yearplot(spread, fillcolor='white', cmap='Greens', linewidth=0.5) plt.show() epidemics = pd.DataFrame({ 'epidemic': ['COVID-19', 'SARS', 'EBOLA', 'MERS', 'H1N1'], 'start_year': [2019, 2003, 2014, 2012, 2009], 'end_year': [2020, 2004, 2016, 2017, 2010], 'confirmed': [full_latest['Confirmed'].sum(), 8096, 28646, 2494, 6724149], 'deaths': [full_latest['Deaths'].sum(), 774, 11323, 858, 19654]
# if you have more kinds of data, get more colormaps from # https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html cmaps = """Blues Oranges Greens Reds""".split() # I want to print 2019 and 2020 data only and for 3 categories one below the other # to see how this year is trending compared to last. for i, yr in enumerate([2020]): for j, cat in enumerate("active confirmed recovered deceased".split()): #we take the events as a series; and fill dates for which #there is no data available with 0 #events = df[df.cat == cat].y.resample("D").asfreq().fillna(0) events = df[cat].resample("D").asfreq().fillna(0) # make the plot and set title k = "{0}_{1}".format(yr, cat) fig[k], ax[k] = plt.subplots(1, 1, figsize=(10, 2)) #tweak figsize x,y calmap.yearplot(events, year=yr, cmap=cmaps[j], daylabels='MTWTFSS', linewidth=1, ax=ax[k]) fig[k].suptitle(cat) #up to here is enough to plot in Jupyter notebook #I wanted to save the plot as pngs too so that those can be #embedded in an html page/email -- the next line saves those if not args.output_prefix: plt.savefig("./{0}_{1}_{2}.png".format(i, j, k)) else: plt.savefig("./{0}_{1}_{2}_{3}.png".format(args.output_prefix, i, j, k))
daily_calories.columns = [ 'sum(calories)', ] daily_calories.sort_index(inplace=True) ##### # calendar # TODO : make a calendar function that is dynamic and isnt terrible like this one year_df = pd.date_range('1/1/2019', periods=365, freq='D') year_df = pd.DataFrame(index=year_df) test = year_df.join(daily_calories) test_series = pd.Series(test.values[:, 0], index=test.index) fig, ax = plt.subplots(figsize=(15, 5)) calmap.yearplot(test_series, year=2019, ax=ax, cmap='Reds', vmin=2000, vmax=3000) fig.savefig('assets/calendar.png', bbox_inches='tight', dpi=75) ##### # functions def datetime_formatter(dt): # today = pd.to_datetime(date.today()) today = pd.to_datetime(datetime.now().date()) yesterday = pd.to_datetime(today - timedelta(days=1)) # dt = pd.to_datetime(dt, format='%d%m%Y') dt = dt.date() # print(today, yesterday, dt)
# https://pythonhosted.org/calmap/ import calmap import matplotlib.pyplot as plt import numpy as np import pandas as pd np.random.seed(sum(map(ord, 'calmap'))) all_days = pd.date_range('1/15/2014', periods=700, freq='D') days = np.random.choice(all_days, 500) events = pd.Series(np.random.randn(len(days)), index=days) calmap.yearplot(events, year=2015) plt.show()
eventList = [0.0] * 365 for event in events: if event.split()[1] == "down": timestamp = int(event.split()[0]) if isInYear(timestamp, year): daynum = timestampToDayNumber(timestamp) eventList[daynum - 1] += 1.0 return pd.Series(eventList, index=days) def isInYear(timestamp, year): return timestampToDate(timestamp).year == year def timestampToDate(timestamp): return datetime.date.fromtimestamp(timestamp) def timestampToDayNumber(timestamp): return datetime.date.fromtimestamp(timestamp).timetuple().tm_yday year = 2017 # change this to the year you want to analyze with open('output.txt') as f: events = f.read().splitlines() downtime = getDowntimeEvents(events, year) calmap.yearplot(downtime, year=year) plt.show()
#!/usr/bin/env python3 import numpy as np import pandas as pd import calmap # Use my fork https://github.com/Te-k/calmap import matplotlib.pyplot as plt import argparse # references # https://pythonhosted.org/calmap/ if __name__ == '__main__': parser = argparse.ArgumentParser( description='Create a heatmap based on csv file') parser.add_argument( 'FILE', help='Csv file, like 2018-07-01;1 for 1 incident that day') parser.add_argument('--sep', '-s', default=';', help='Separator for the csv file (default is ;)') args = parser.parse_args() df = pd.read_csv(args.FILE, sep=args.sep, header=None) dates = pd.to_datetime(df[0]) events = pd.Series(np.array(df[1]), index=dates) calmap.yearplot(events, year=min(dates).year) plt.show()
def test_yearplot_year(events): """ We can choose which year is plotted with the `year` keyword argment. """ ax = calmap.yearplot(events, year=2015) return ax.figure
def test_yearplot(events): """ By default, `yearplot` plots the first year and sums the values per day. """ ax = calmap.yearplot(events) return ax.figure
def create_heatmap(ax, fig, series, year=2020, cmap='YlGn'): cax = calmap.yearplot(series, year=2020, ax=ax, cmap=cmap) fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal') set_axis_sizes(ax)
# for dt in rrule(DAILY, dtstart=start_date, until=end_date): # plt.axvline(x=dt.strftime("%Y-%m-%d")) # if dt.strftime("%Y-%m-%d") in anomalies: # plt.axvspan(dt, dt+ timedelta(days=1), color='r', alpha=0.2) # OSCVMS = np.unique(data_analysed[data_analysed[(str(classifiers[jo][0]).split("(")[0])] == 1].index.date.astype(str)) # print((str(classifiers[jo][0]).split("(")[0])) # print(OSCVMS) #%% for _ in range(1, 5): fig = plt.figure(figsize=(10, 4)) ax = fig.add_subplot(111) events = pd.Series(data_analysed.iloc[:, (_)].values, index=data_analysed.index) cax = calmap.yearplot(events, year=start_date.year, linecolor='grey', cmap='Oranges', ax=ax) fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal') # fig.show() #%% for jo in classi: clf.fit(X_train) b = 0 ans = 0 for i in range(1, 100): a = np.sum(((clf.predict_proba(X_test)[:, 1]) >= (i / 100) ).astype(int) == clf.predict(X_test)) if a > b: b = a ans = i
import pandas as pd import calmap import matplotlib.pyplot as plt df = pd.read_csv('downtimes.log') df = df.drop(['checked_servers', 'cmd', 'status'], axis=1) # Create Series from count of dates in log per day df["date"] = df["date"].apply(pd.to_datetime) df['day'] = df['date'].apply(lambda x: "%d/%d/%d" % (x.day, x.month, x.year)) df['day']= pd.to_datetime(df['day']) df = df.groupby(['day']).count() df = df.iloc[:,0] # plot heatmap calmap.yearplot(df, year=2019) plt.savefig('./heatmap.png')
columns=['Day', 'Event', 'Location', 'Count']) for i, row in df.iterrows() ], ignore_index=True) df.set_index('Day', inplace=True) print(df) fig, ax = plt.subplots(figsize=(15, 10)) calmap.yearplot(df['Count'], year=2019, ax=ax, how=None, vmin=0, vmax=1, monthseparator=True, separatorwidth=2, fillcolor=zero_colour, linecolor=bg_colour, cmap=cm, separatorcolor=sep_colour) ax.tick_params(axis='both', colors=highlight_colour) ax.tick_params(axis='x', labelsize=20) ax.tick_params(axis='y', labelsize=15) title = ax.set_title('2019 Gig Calendar', fontweight='bold', color=highlight_colour, size=40)
import matplotlib.pyplot as plt import pandas as pd import numpy as np import calmap data = pd.read_csv("Results.csv") all_days = pd.date_range('1/1/2015', periods=365, freq='D') all_days = np.array(all_days.to_pydatetime(), dtype=np.datetime64) events = pd.Series(data["Values"].values, index=all_days) fig = plt.figure(figsize=(20,8)) ax = fig.add_subplot(111) cax = calmap.yearplot(events, year=2015, ax=ax, cmap='rainbow') fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal') plt.show()