# In[3]: """create a heatmap of daily total steps burned""" allFiles = glob.glob("Data/Activities_Summary/*.csv") # create a list of all data files fulldf = [] for file_ in allFiles: # for loop to merge data files df = pd.read_csv(str(file_), index_col=0) fulldf.append(df) fulldf = pd.concat(fulldf) fulldf.index=pd.to_datetime(fulldf.index) # use date as index fulldf = fulldf[fulldf.steps != 0] # remove days without data events = pd.Series(fulldf['steps']) # create a heat map of steps walked per day fig,ax=calmap.calendarplot(events, monthticks=True, cmap='GnBu', vmin=0, vmax=max(fulldf['steps'])+1000, fillcolor='gainsboro', linecolor='white', linewidth=2, fig_kws=dict(figsize=(12, 8))); cax = fig.add_axes([1.0, 0.2, 0.02, 0.6]) norm1 = mpl.colors.Normalize(0,max(fulldf['steps'])+1000) cb = mpl.colorbar.ColorbarBase(cax, cmap='GnBu', norm=norm1, spacing='proportional') cb.set_label('# of Steps') # In[4]: dayofweek = [] for day in fulldf.index: # create new column for day of week #datentime=dt.datetime.strptime(day, '%Y-%m-%d') #dateonly=datentime.date() dateonly=day.date()
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 create_dfs(): try: df = pd.read_pickle("./castellion_stats.pkl") df2 = pd.read_pickle("./castellion_winloss.pkl") except: df = pd.DataFrame({'Date': today, 'Count': 1}, index=[0]) df2 = pd.DataFrame({'Wins': 0, 'Games': 1}, index=[0]) dates = [today] if df.Date.isin(dates).any(): df.loc[df.Date.isin(dates), 'Count'] += 1 else: df = df.append({'Date': today, 'Count': 1}, ignore_index=True) df2['Games'][0] += 1 print(df2['Games']) df['Date'] = pd.to_datetime(df['Date']) df.to_pickle("./castellion_stats.pkl") df2.to_pickle("./castellion_winloss.pkl") df.set_index('Date', inplace=True) plt.figure(figsize=(16, 10), dpi=80) calmap.calendarplot(df['2021']['Count'], fig_kws={'figsize': (16, 10)}, yearlabel_kws={ 'color': 'black', 'fontsize': 14 }, subplot_kws={'title': 'Days Played'}, cmap='YlOrRd') plt.savefig('heatmap.png', bbox_inches='tight') return df2
def calmap(): if os.path.exists( os.path.join(root_path, "Daily Data", "Portfolio", "Portfolio_Returns.csv")): port_rets = pd.read_csv(os.path.join(root_path, "Daily Data", "Portfolio", "Portfolio_Returns.csv"), index_col=0) import numpy as np np.random.seed(sum(map(ord, 'calmap'))) import calmap events = port_rets['Portfolio Value'] events.index = pd.to_datetime(events.index) #calmap.yearplot(events, year=2018) 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.show() else: print( 'You have not downloaded data for your portfolio yet in oder for the optimization module to be run. Please download the data by running the following function --- port_data.portfolio_daily_data()' )
def _plot_calendar(events, cmap="Reds", fillcolor="whitesmoke", **kwargs): import calmap # Filter away journal entries and sort events = list(sorted(filter(lambda e: e.type == "data", events))) for e in events: e.data["substance"] = "Any" period_counts = _count_doses(events, one_per_day=True, monthly=False) assert len(period_counts) == 1 doses = [n_dose for n_dose in next(iter(period_counts.values())).values()] labels = [ pd.Timestamp("-".join(map(str, date))) for sd in period_counts for date in period_counts[sd].keys() ] series = pd.Series(doses, index=labels) series = series[~series.index.duplicated()] series = series.resample("D").sum().asfreq("D") # print(series.tail(20)) calmap.calendarplot(series, fillcolor=fillcolor, cmap=cmap, linewidth=1, fig_kws=kwargs) plt.show()
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 calendarImg(dataset): for i, id_input in enumerate(dataset.id): df = calendarDF[calendarDF.listing_id.isin([id_input])] df['available_num'] =[3 if x ==1 else -1 for x in df['available']] # instead of t and f, it will be 3 and -1 df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d').dt.strftime('%Y-%m-%d %H:%M:%S') # convert date format with h:m:s events=pd.Series(df['available_num']) # convert the dataset into series events.index= pd.DatetimeIndex(df['date']) # set date as the index calmap.calendarplot(events, monthticks=1, daylabels='MTWTFSS',cmap='YlGn', fillcolor='grey', linewidth=1, fig_kws=dict(figsize=(16, 4))) name = "img"+str(i)+".png" plt.savefig(name)
def Calendar_Heat_Map(self): df = pd.read_csv("dataVset/yahoo.csv", parse_dates=['date']) df.set_index('date', inplace=True) # Plot # plt.figure(figsize=(16, 10), dpi=80) calmap.calendarplot(df['2014']['VIX.Close'], fig_kws={'figsize': (16, 10)}, yearlabel_kws={ 'color': 'black', 'fontsize': 14 }, subplot_kws={'title': 'Yahoo Stock Prices'}) plt.show()
def plot_calmap(events=_get_random_events()): # cmap choices: viridis calmap.calendarplot( events, monthticks=True, daylabels="MTWTFSS", cmap="PuBuGn", fillcolor="whitesmoke", linewidth=1, vmin=0, fig_kws=dict(figsize=(12, 6)), fig_suptitle="CTimer clock count", ) plt.show()
def calendar_plot(overall_dict, dates, filename): names = list(overall_dict.keys()) weights = [i + 1 for i in range(len(names))] daily_weights = list() for day_index in range(len(dates)): min = 999999 weight = None for name in names: if overall_dict[name][day_index] is not None: if overall_dict[name][day_index] < min: min = overall_dict[name][day_index] weight = weights[names.index(name)] elif overall_dict[name][day_index] == min: weight = 0 daily_weights.append(weight) datetimes = [ dt.datetime(year=int(date.split('/')[2]), month=int(date.split('/')[0]), day=int(date.split('/')[1])) for date in dates ] series = pd.Series(data=daily_weights, index=datetimes) cmap = matplotlib.colors.ListedColormap( ['grey', 'skyblue', 'navajowhite', 'palegreen', 'lightcoral', 'plum']) fig, ax = calmap.calendarplot(series, fillcolor='silver', cmap=cmap, fig_kws=dict(figsize=(10, 4))) labels = ['Tie'] + names formatter = plt.FuncFormatter(lambda val, loc: labels[int(val)]) fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist(), shrink=0.4, format=formatter) plt.savefig(filename) plt.close('all')
def plot_yoy_growth(self): # CALENDAR MAP IMAGE calmap.calendarplot( self.yoy_growth.iloc[self.yoy_growth.index.year > 2016]['index'], monthticks=1, daylabels='MTWTF', dayticks=[0, 2, 4], cmap='RdYlGn', linewidth=0.2, yearascending=False, yearlabel_kws=dict(color='#696969'), fig_kws=dict(figsize=(12, 8))) plt.savefig('assets/yoy_calmap.png') # YOY GROWTH - show different colour for positive and negative yoy growth postive_growth = go.Scatter( y=[0 if x < 0 else x for x in self.yoy_growth['28dayMA']], x=self.yoy_growth.index, fill='tozeroy', line=dict(color='#2F80ED')) negative_growth = go.Scatter( y=[0 if x > 0 else x for x in self.yoy_growth['28dayMA']], x=self.yoy_growth.index, fill='tozeroy') baseline = go.Scatter(y=[0 for x in self.yoy_growth.index], x=self.yoy_growth.index, line=dict(color='black')) layout = go.Layout(plot_bgcolor='#ffffff', yaxis=dict(tickformat="%"), xaxis=dict(range=([ self.yoy_growth.index.min(), self.yoy_growth.index.max() + pd.DateOffset(months=6) ])), showlegend=False, hovermode='x') yoy_growth_chart = go.Figure( data=[postive_growth, negative_growth, baseline], layout=layout) return yoy_growth_chart
def calheatmap(year): ''' Plot a Calendar heat map. Number of citations vs Date in a specific year Input: year: year in [2015, 2016, 2017, 2018] ''' assert year in [2015, 2016, 2017, 2018] df = pd.read_csv(str(year) + 'parking-citations.csv', parse_dates=['Issue Date']) gp = df['Fine amount'].groupby(df['Issue Date']) calmap.calendarplot( gp.count(), fig_kws={'figsize': (16, 10)}, yearlabels=False, subplot_kws={'title': 'Number of Citations in Year ' + str(year)}) plt.show()
def plotCalendar(df): for column in df.columns: # https://pythonhosted.org/calmap/ fig, ax = calmap.calendarplot( df[column], daylabels='MTWTFSS', cmap='RdYlGn') #The _r at the end inverts the map, so 1 is green. plt.title(column) plt.legend() plt.savefig(column + '.png') return
def calmap(port_rets): import numpy as np np.random.seed(sum(map(ord, 'calmap'))) import calmap events = port_rets['Portfolio Value'] events.index = pd.to_datetime(events.index) #calmap.yearplot(events, year=2018) 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.show()
def plot_daily_flow_rate_average( df: pd.DataFrame, factory: str, flare: str, max_rates: MaxRateCollection, bound_scale: bool, filter_run: dict = None, **kwargs ) -> Optional[Plot]: """Plot heat-map for flare's average hourly flow rate per-day""" if is_skip_plot(dict(type='plot_daily_flow_rate_average', factory=factory, flare=flare), filter_run): return None max_hourly_rate = max_rates.hourly if max_rates else None if bound_scale and not max_hourly_rate: # Bounded plot can only be made on hourly-limited flare return None # Build title flare_title = get_flare_title(flare, factory) title = f'ממוצע יומי של ספיקה שעתית {flare_title}' title += '\nביחידות ק"ג/שעה' if not max_hourly_rate: title += f' {NO_LAW_STR}' elif not bound_scale: title += f' {law_str_unbounded_func(max_hourly_rate.rate)}' else: title += f' {law_str_func(max_hourly_rate.rate)}' # Plot graph num_years = len(df.sum(axis=1).resample('Y').sum()) # there must be a more elegant way to do this! fig, axes = calmap.calendarplot( df[flare], how='mean', # Hourly vmax=max_hourly_rate.rate if bound_scale else None, fillcolor='grey', linewidth=0, fig_kws=dict(figsize=(15, 2 + 3 * num_years)), cmap=kwargs.pop('cmap', plt.cm.get_cmap(colormap_negative_name)), **kwargs ) fig.suptitle(get_display(title), fontsize=30) # Plot color bar fig.colorbar(axes[0].get_children()[1], ax=axes.ravel().tolist(), orientation='horizontal', shrink=1.0, extend='max' if bound_scale else 'neither' ) return Plot(fig, title, df)
def main(): df = (pd.read_excel('Growth_Without_Goals.xlsx', sheetname='Log')).set_index('Date') for column in df.columns: # https://pythonhosted.org/calmap/ fig, ax = calmap.calendarplot(df[column], daylabels='MTWTFSS', cmap='RdYlGn') plt.title(column) plt.savefig(column + '.png')
def calplot_funk(df, keycol, filename_base): '''docstring for calplot_funk function''' fig, ax = calmap.calendarplot( df[keycol], fillcolor='grey', # linewidth=0,#cmap='RdYlGn', fig_kws=dict(figsize=(17, 12)), subplot_kws={'title': keycol}, vmin=0) fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist(), orientation='horizontal') filename = filename_base + '_' + keycol + '.png' plt.savefig(filename) return plt.show()
def calendarPlot(): import calmap df = pd.read_csv( "https://raw.githubusercontent.com/swapnilsaurav/Dataset/master/stock_price.csv" ) # Prepare the data for plotting # print("df= \n", df) df["date"] = pd.to_datetime(df["date"]) # print("df[date]\n", df["date"]) # The data must be a series with a date time index df.set_index("date", inplace=True) x = df[df["year"] == 2016]["Close"] print("x=\n", x) # Plot the data using calmap calmap.calendarplot(x, fig_kws={'figsize': (16, 10)}, yearlabel_kws={ "color": "black", "fontsize": 14 }, subplot_kws={"title": "Stock Prices"}) plt.show()
def calmap(portfolio): port_rets = pd.read_csv(root_path + '/Daily_Data/Portfolio/' + portfolio.Portfolio_Name + '_Portfolio_Returns.csv', index_col=0) import numpy as np np.random.seed(sum(map(ord, 'calmap'))) import calmap events = port_rets['Portfolio Value'] events.index = pd.to_datetime(events.index) #calmap.yearplot(events, year=2018) 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.show()
def plot_daily_number_of_exceptions_from_allowed_hourly_flow_rate_average( df: pd.DataFrame, factory: str, max_rates: Optional[MaxRateCollection], filter_run: dict = None, **kwargs ) -> Optional[Plot]: """Plot heat-map for number of exceptions per-day""" if is_skip_plot(dict(type='plot_daily_number_of_exceptions_from_allowed_hourly_flow_rate_average', factory=factory, flare=None), filter_run): return None if not max_rates: return None max_hourly_rate = max_rates.hourly if not max_hourly_rate: return None max_total_hourly_rate = max_hourly_rate.rate df = df.copy() df['Exception'] = df['Total'] > max_total_hourly_rate num_years = len(df.sum(axis=1).resample('Y').sum()) # there must be a more elegant way to do this! fig, axes = calmap.calendarplot( df['Exception'], fillcolor='grey', linewidth=0, fig_kws=dict(figsize=(15, 3 + 3 * num_years)), cmap=kwargs.pop('cmap', plt.cm.get_cmap(colormap_negative_name, 24)), **kwargs ) cbar = fig.colorbar(axes[0].get_children()[1], ax=axes.ravel().tolist(), orientation='horizontal', shrink=1.0, ticks=[0, 6, 12, 18, 24]) # cbar.ax.get_yaxis().labelpad = 15 cbar.ax.set_xlabel(get_display('שעות חריגה ביום'), fontsize=20) title = f'כמות חריגות יומיות מהיתר ספיקה שעתית בלפידי {factory}' title += '\nביחידות שעות חריגה ליום' fig.suptitle(get_display(title), fontsize=30) return Plot(fig, title, df)
def plot_calendar_heatmap(data, width=800, height=200, style='whitegrid', format=SVG, cache=True): fig, ax = calmap.calendarplot(data, how=None, dayticks=(0, 2, 4, 6), yearlabel_kws={ 'color': 'black', 'fontsize': 10, 'fontweight': 'normal' }, cmap='YlGn', fillcolor='#eeeeee') return fig, width, height * len(data.index.year.unique())
def heatmap_data(data1, data2, data3): value1 = [] all_day = pd.date_range('01/01/2017', periods=1095, freq='1D') for i in range(365): value1.append(data1["Value"][i]) for i in range(365): value1.append(data2["Value"][i]) for i in range(365): value1.append(data3["Value"][i]) events = pd.Series(value1, index=all_day) fig, ax = calmap.calendarplot(events, linewidth=0, fig_kws=dict(figsize=(17, 8))) fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist()) plt.show(fig)
numPerDay = 0 else: numPerDay = numPerDay + 1 print(nArr) newArray = np.asarray(nArr) print(newArray) df = pd.DataFrame({'Date': newArray[:, 0], 'AppNum': newArray[:, 1]}) print(df) df.set_index('Date', inplace=True) # Plot plt.figure(figsize=(16, 10), dpi=80) calmap.calendarplot(df[year[i]]['AppNum'], fig_kws={'figsize': (16, 10)}, yearlabel_kws={ 'color': 'black', 'fontsize': 14 }, subplot_kws={ 'title': '{} Diligence of Sending Applications'.format( year[i]) }) plt.show()
allFiles = glob.glob( "Data/Activities_Summary/*.csv") # create a list of all data files fulldf = [] for file_ in allFiles: # for loop to merge data files df = pd.read_csv(str(file_), index_col=0) fulldf.append(df) fulldf = pd.concat(fulldf) fulldf.index = pd.to_datetime(fulldf.index) # use date as index fulldf = fulldf[fulldf.steps != 0] # remove days without data events = pd.Series(fulldf['steps']) # create a heat map of steps walked per day fig, ax = calmap.calendarplot(events, monthticks=True, cmap='GnBu', vmin=0, vmax=max(fulldf['steps']) + 1000, fillcolor='gainsboro', linecolor='white', linewidth=2, fig_kws=dict(figsize=(12, 8))) cax = fig.add_axes([1.0, 0.2, 0.02, 0.6]) norm1 = mpl.colors.Normalize(0, max(fulldf['steps']) + 1000) cb = mpl.colorbar.ColorbarBase(cax, cmap='GnBu', norm=norm1, spacing='proportional') cb.set_label('# of Steps') # In[4]: dayofweek = []
# -*- coding: utf-8 -*- """ Created on Sat Nov 30 20:15:47 2019 @author: Jie Zhang,微信公众号【EasyShu】,本代码源自《Python数据可视化之美》 """ import pandas as pd import numpy as np from plotnine import * import matplotlib.pyplot as plt import calmap df = pd.read_csv('Calendar.csv', parse_dates=['date']) df.set_index('date', inplace=True) fig, ax = calmap.calendarplot(df['value'], fillcolor='grey', linecolor='w', linewidth=0.1, cmap='RdYlGn', yearlabel_kws={ 'color': 'black', 'fontsize': 12 }, fig_kws=dict(figsize=(10, 5), dpi=80)) fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist()) plt.show() #fig.savefig('日历图1.pdf')
# 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.calendarplot(events, monthticks=3, daylabels='MTWTFSS', dayticks=[0, 2, 4, 6], cmap='YlGn', fillcolor='grey', linewidth=0, fig_kws=dict(figsize=(8, 4))) plt.show()
# https://github.com/martijnvermaat/calmap # http://stackoverflow.com/a/11274226 # http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.from_csv.html # http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.sort_index.html import os # generating matplotlib graphs without a x-server # http://stackoverflow.com/a/4935945 import matplotlib as mpl mpl.use('Agg') import calmap import matplotlib.pyplot as plt import pandas csv_file = os.getcwd() + "/../data/steps-count.csv" events = pandas.Series.from_csv(csv_file) events = events.sort_index(ascending=False) ax = calmap.calendarplot(events, yearascending=False, daylabels='mtwtfss', linewidth=0.5, vmax=10000, vmin=1, cmap='Greens', fig_kws=dict(figsize=(8, 4))) plt.setp(ax) # todo: title always showing up in the last calmap/subplot # plt.title("Total Daily Steps Heatmap") plt.savefig("total_daily_steps_heatmap.png")
import matplotlib.pyplot as plt pd.set_option('display.max_columns', 20) # KOSPI 데이터 준비 df = pd.read_excel('./data/kospi.xls', parse_dates=['년/월/일']) print(df.head(), '\n') df.columns = [ 'date', 'price', 'up_down', 'change', 'start', 'high', 'low', 'vol_num', 'vol_amt', 'mkt_cap' ] df = df.set_index('date', drop=True) print(df.head(), '\n') # Calendar Map 표현 plt.figure(figsize=(16, 8)) calmap.calendarplot( df.change, monthticks=1, daylabels='MTWTFSS', dayticks=[0, 2, 4, 6], cmap='YlGn', linewidth=0.05, fillcolor='grey', fig_kws=dict(figsize=(14, 6)), yearlabel_kws=dict(color='black', fontsize=12), subplot_kws=dict(title='2018 KOSPI Price Trend'), ) plt.show()
import matplotlib as mpl import calmap import pandas as pd import matplotlib.pyplot as plt # Import Data df = pd.read_csv( "https://raw.githubusercontent.com/selva86/datasets/master/yahoo.csv", parse_dates=['date']) df.set_index('date', inplace=True) # Plot plt.figure(figsize=(16, 10), dpi=80) calmap.calendarplot(df['2014']['VIX.Close'], fig_kws={'figsize': (16, 10)}, yearlabel_kws={ 'color': 'black', 'fontsize': 14 }, subplot_kws={'title': 'Yahoo Stock Prices'}) plt.show()
def test_calendarplot(events): """ With `calendarplot` we can plot several years in one figure. """ fig, axes = calmap.calendarplot(events) return fig
def dbplot(): data = pd.read_excel("C:\\Users\\user\\Desktop\\death_criminal.xlsx") data2 = pd.read_excel("C:\\Users\\user\\Desktop\\death_police.xlsx") #state = data['State'] state_data = Victims.objects.all() state_list = [] for i in state_data: state_list = i.state state = pd.DataFrame(state_list) state_list = list(set(state)) state_list_count = {} state_list_prob = {} for i in state_list: state_list_count[i] = 0 for i in range(0, len(data)): state_data = data['State'][i] if state_data in state_list: state_list_count[state_data] += 1 for i, j in state_list_count.items(): state_list_prob[i] = j / len(data) state_list_count2 = sorted(state_list_count.items(), reverse=True, key=lambda item: item[1]) x = [] y = [] for i in range(0, 20): x.append(state_list_count2[i][0]) y.append(state_list_count2[i][1]) plt.figure(figsize=(10, 5)) plt.bar(x[0:20], y[0:20], color='red') plt.xlabel('state') plt.savefig( 'C:/Users/user/Desktop/dbproject/dbwebprograming/rip_floyd/static/img/plot1.png' ) ##날짜 그래프 col1 = 'Date of Incident (month/day/year)' date_list = list(set(data[col1])) date_count = {} for i in range(0, len(data)): date_data = data[col1][i] if date_data in date_count: date_count[date_data] += 1 else: date_count[date_data] = 1 days = list(date_count.keys()) happens = list(date_count.values()) events = pd.Series(happens, index=days) calmap.calendarplot(events) plt.savefig( 'C:/Users/user/Desktop/dbproject/dbwebprograming/rip_floyd/static/img/plot2.png' ) ##인종별 racial_list = list(set(data["Victim's race"])) racial_list_count = {} for i in range(0, len(data)): racial_data = data["Victim's race"][i] if racial_data in racial_list_count: racial_list_count[racial_data] += 1 else: racial_list_count[racial_data] = 1 plt.figure(figsize=(10, 5)) plt.bar(list(racial_list_count.keys())[0:4], list(racial_list_count.values())[0:4], color='red') plt.xlabel('Racial') plt.savefig( 'C:/Users/user/Desktop/dbproject/dbwebprograming/rip_floyd/static/img/plot3.png' ) ##경찰들 순직사건 주별로 police_list_count = {} for i in range(0, len(data2)): data2['state'][i] = data2['state'][i].split(', ')[1] for i in range(0, len(data2)): info = data2['state'][i] if info in police_list_count: police_list_count[info] += 1 else: police_list_count[info] = 1 police_list_count2 = sorted(police_list_count.items(), reverse=True, key=lambda item: item[1]) x2 = [] y2 = [] for i in range(0, 20): x2.append(police_list_count2[i][0]) y2.append(police_list_count2[i][1]) plt.figure(figsize=(10, 5)) plt.bar(x2[0:20], y2[0:20], color='red') plt.xlabel('State') plt.savefig( 'C:/Users/user/Desktop/dbproject/dbwebprograming/rip_floyd/static/img/plot4.png' ) ##경찰들의 총기순직사건과 범죄자들의 과잉진압 사건 회귀분석 x1_value = [] #state별 범죄사망자 x2_value = [] #state별 경찰사망자 for i in state_list: x1_value.append(state_list_count.get(i)) x2_value.append(police_list_count.get(i)) reg_x1 = pd.DataFrame(x1_value, index=state_list, columns=['events']) reg_x2 = pd.DataFrame(x2_value, index=state_list, columns=['events']) reg_x2 = reg_x2.fillna(0) reg = LinearRegression() reg.fit(reg_x1, reg_x2) r2 = reg.score(reg_x1, reg_x2) y_pred = reg.predict(reg_x1) plt.scatter(reg_x1, reg_x2) plt.plot(reg_x1, y_pred) plt.savefig( 'C:/Users/user/Desktop/dbproject/dbwebprograming/rip_floyd/static/img/plot5.png' )
temp.rename(columns={'raining': 'hours_raining'}, inplace=True) temp['day'] = temp['day'].apply(lambda x: x.to_datetime().date()) rainy_days = rainy_days.merge(temp, left_on='date', right_on='day', how='left') rainy_days.drop('day', axis=1, inplace=True) print ("In the year, there were {} rainy days of {} at {}".format(rainy_days['rain'].sum(), len(rainy_days), station) ) print ("It was wet while cycling {} working days of {} at {}".format(wet_cycling['get_wet_cycling'].sum(), len(wet_cycling), station)) print ("You get wet cycling {} % of the time!!".format(wet_cycling['get_wet_cycling'].sum()*1.0*100/len(wet_cycling))) import calmap temp = rainy_days.copy().set_index(pd.DatetimeIndex(analysis['rainy_days']['date'])) #temp.set_index('date', inplace=True) fig, ax = calmap.calendarplot(temp['hours_raining'], fig_kws={"figsize":(15,4)}) plt.title("Hours raining") fig, ax = calmap.calendarplot(temp['total_rain'], fig_kws={"figsize":(15,4)}) plt.title("Total Rainfall Daily") temp[['get_wet_cycling', 'total_rain', 'hours_raining']].plot() def analyse_station(data_raw, station): """ Function to analyse weather data for a period from one weather station. Args: data_raw (pd.DataFrame): Pandas Dataframe made from CSV downloaded from wunderground.com station (String): Name of station being analysed (for comments) Returns: