def create_and_save_graph(df): # Filling NaN with 0 df = df.fillna(0) # Selecting 11th row and 1st column df1_label = df.iloc[10:, :1] # Converting dataframe into list df1_label = df1_label.values.tolist() plt.gcf().subplots_adjust(bottom=0.15) trend_graph_path = os.path.join(os.path.dirname(__file__), 'shivam.png') # Select data for Grand Total Plot df1 = df.iloc[10:, 1:9] df1 = df1.iloc[0:, 1:] # Plot first graph ax = df1.T.plot(kind='line', marker='s', x_compat=True, rot=0, alpha=0.75, grid=True) # Data for second graph df = df.iloc[:10, :9] df = df.drop(["Category"], axis=1) df_label = df.iloc[:, 0] df_label = df_label.values.tolist() df = df.iloc[0:, 1:] ax = df.T.plot(kind='bar', width=0.9, legend=False, figsize=(60, 4), ax=ax, rot=0, alpha=0.75, grid=True, colormap='Paired') x_offset = -0.02 y_offset = 70.02 for p in ax.patches: b = p.get_bbox() val = int(b.y1 + b.y0) ax.annotate(val, ((b.x0 + b.x1) / 2 + x_offset, b.y1 + y_offset), rotation=90) patches, labels = ax.get_legend_handles_labels() labels = df1_label + df_label ax.legend(patches, labels, loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=11, fancybox=True, shadow=True, prop={'size': 10}) plt.savefig(trend_graph_path, bbox_inches='tight', dpi=200)
def annotate_value(df, current_feature, value, axes): """ Annotate given value of given feature on chart. Assumed it is available. """ x_val = df[current_feature].values.tolist().index(value) y_val = value #print x_val,y_val axes.annotate('Outlier!', xy=(x_val, y_val * 0.95), xytext=(x_val, y_val * 0.80), arrowprops=dict(facecolor='black')) return axes
def add_labels(ax, bars): """ Description: Inputs: Outputs: """ for bar in bars: height = bar.get_height() ax.annotate('{}'.format(height), xy=(bar.get_x() + bar.get_width() / 2, height), xytext=(0, 3), # 3 points vertical offset textcoords="offset points", ha='center', va='bottom')
def club_tags(unencoded_clubs_list): """ Creates a bar graph showing of the number of occurrances of each possible club tag. By using a dictionary to count club tag occurrances, the function then creates a matplotlib bar graph using numpy representations of the dictionary information. The graph is formatted and pushed as a Response type for view on the server. Returns: -------- Response The graph """ club_dict = {} for clubs in unencoded_clubs_list: for tag in clubs.get_category(): if tag in club_dict: club_dict[tag] = club_dict[tag] + 1 else: club_dict[tag] = 1 x = np.zeros(len(club_dict)) index_counter = 0 for tag in club_dict: x[index_counter] = club_dict.get(tag) index_counter = index_counter + 1 fig = plt.figure() ax = fig.add_subplot() bar = ax.bar(np.arange(len(club_dict)), x) labels = club_dict.keys() ax.set_xticks(np.arange(len(club_dict))) ax.set_xticklabels(labels, rotation='45', ha='right') ax.set_xlabel('Club Tags') ax.set_ylabel('Number of Occurrances') ax.set_title('Number of Club Tag Occurrances') for rect in bar: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords="offset points", ha='center', va='bottom') plt.tight_layout() output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype='image/png')
def clubs_per_user(user_list): """ Creates a bar graph of the number of clubs per user. Using a dictionary to gather each user's name and the number of club each user is in, numpy representations of the data is used to create a matplotlib bar graph. The graph is then formatted and pushed for view on the server. Returns: -------- Response The graph """ user_club_dict = {} for user in user_list: name = user.get_user_name() user_club_dict[name] = len(user.get_user_clubs()) x = np.zeros(len(user_club_dict)) index_counter = 0 for user in user_club_dict: x[index_counter] = user_club_dict.get(user) index_counter = index_counter + 1 fig = plt.figure() ax = fig.add_subplot() bar = ax.bar(np.arange(len(user_club_dict)), x) labels = user_club_dict.keys() ax.set_xticks(np.arange(len(user_club_dict))) ax.set_xticklabels(labels, rotation='45', ha='right') ax.set_xlabel('User Name') ax.set_ylabel('Number of Clubs') ax.set_title('Number of Clubs per User') for rect in bar: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords="offset points", ha='center', va='bottom') plt.tight_layout() output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype='image/png')