def distribution(): mu, sigma = 0, 0.5 measured = np.random.normal(mu, sigma, 1000) hist, edges = np.histogram(measured, density=True, bins=20) x = np.linspace(-2, 2, 1000) pdf = 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) cdf = (1 + scipy.special.erf((x - mu) / np.sqrt(2 * sigma ** 2))) / 2 output_server("distribution_reveal") hold() figure(title="Interactive plots", tools="pan, wheel_zoom, box_zoom, reset, previewsave", background_fill="#E5E5E5") quad(top=hist, bottom=np.zeros(len(hist)), left=edges[:-1], right=edges[1:], fill_color="#333333", line_color="#E5E5E5", line_width=3) # Use `line` renderers to display the PDF and CDF line(x, pdf, line_color="#348abd", line_width=8, alpha=0.7, legend="PDF") line(x, cdf, line_color="#7a68a6", line_width=8, alpha=0.7, legend="CDF") xgrid().grid_line_color = "white" xgrid().grid_line_width = 3 ygrid().grid_line_color = "white" ygrid().grid_line_width = 3 legend().orientation = "top_left" return curplot(), cursession()
def tweetsGraph(): logger.info("Drawing graphs to %s" % path_to_graphs+"Stats.html") stat_db_cursor.execute('SELECT * FROM tweets') tweets = stat_db_cursor.fetchall() date, volume, cumulative, volumePast24h = zip(*[(datetime.datetime.strptime(t['date'], "%Y-%m-%dT%H"), t['current_hour'], t['cumulative'], t['past_24h']) for t in tweets]) hourly =zip([datetime.datetime(year=d.year, month=d.month, day=d.day) for d in date],volume) hourly.sort() days, dailyVolume = zip(*[(d, sum([v[1] for v in vol])) for d,vol in itertools.groupby(hourly, lambda i:i[0])]) bokeh_plt.output_file(path_to_graphs+"Stats.html") bokeh_plt.hold() bokeh_plt.quad(days, [d+datetime.timedelta(days=1) for d in days], dailyVolume, [0]*len(dailyVolume), x_axis_type="datetime", color='gray', legend="Daily volume") bokeh_plt.line(date, volume, x_axis_type="datetime", color='red', legend="Hourly volume") bokeh_plt.line(date, volumePast24h, x_axis_type="datetime", color='green', legend="Volume in the past 24 hours") bokeh_plt.curplot().title = "Volume" bokeh_plt.figure() bokeh_plt.line(date, cumulative, x_axis_type="datetime") bokeh_plt.curplot().title = "Cumulative volume" fig, ax = matplotlib_plt.subplots() f=DateFormatter("%Y-%m-%d") ax.xaxis.set_major_formatter(f) matplotlib_plt.plot(date, volume) matplotlib_plt.plot(date, volumePast24h) matplotlib_plt.plot(days, dailyVolume) matplotlib_plt.xticks(np.concatenate((np.array(date)[range(0,len(date),24*7)],[date[-1]])), rotation=70) matplotlib_plt.savefig(path_to_graphs+"volume.png", bbox_inches="tight") stat_db_cursor.execute('SELECT * FROM users') users = stat_db_cursor.fetchall() date, nUsers, nUsersWithFriends = zip(*[(datetime.datetime.strptime(u['date'], "%Y-%m-%dT%H:%M:%S.%f"), u['total'], u['with_friends']) for u in users]) bokeh_plt.figure() bokeh_plt.line(date, nUsers, x_axis_type="datetime", legend="Total") bokeh_plt.line(date, nUsersWithFriends, x_axis_type="datetime", legend="Friendship collected") bokeh_plt.legend().orientation = "top_left" bokeh_plt.curplot().title = "Number of users" bokeh_plt.save() matplotlib_plt.figure() fig, ax = matplotlib_plt.subplots() f=DateFormatter("%Y-%m-%d") ax.xaxis.set_major_formatter(f) matplotlib_plt.plot(date, nUsers) matplotlib_plt.plot(date, nUsersWithFriends) matplotlib_plt.xticks(np.concatenate((np.array(date)[range(0,len(date),24*7)],[date[-1]])), rotation=70) matplotlib_plt.savefig(path_to_graphs+"users.png", bbox_inches="tight")
def distribution(): mu, sigma = 0, 0.5 measured = np.random.normal(mu, sigma, 1000) hist, edges = np.histogram(measured, density=True, bins=20) x = np.linspace(-2, 2, 1000) pdf = 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-(x - mu)**2 / (2 * sigma**2)) cdf = (1 + scipy.special.erf((x - mu) / np.sqrt(2 * sigma**2))) / 2 output_server("distribution_reveal") hold() figure(title="Interactive plots", tools="pan, wheel_zoom, box_zoom, reset, previewsave", background_fill="#E5E5E5") quad(top=hist, bottom=np.zeros(len(hist)), left=edges[:-1], right=edges[1:], fill_color="#333333", line_color="#E5E5E5", line_width=3) # Use `line` renderers to display the PDF and CDF line(x, pdf, line_color="#348abd", line_width=8, alpha=0.7, legend="PDF") line(x, cdf, line_color="#7a68a6", line_width=8, alpha=0.7, legend="CDF") xgrid().grid_line_color = "white" xgrid().grid_line_width = 3 ygrid().grid_line_color = "white" ygrid().grid_line_width = 3 legend().orientation = "top_left" return curplot(), cursession()