Generation

generate functionMon, 17 Apr 2023

def plot_graph(chart_title, column_to_label): df=show_data.sort_values(by="Upload", ascending=False)[:5] ax = df.plot(kind='barh', figsize=(20,15), x="Name", rot=0) plt.title(chart_title, fontsize=20) plt.xlabel("") plt.ylabel("") plt.xticks(fontsize=20) plt.yticks(fontsize=20) #adding value labels in the center of each bar for p in ax.patches: ax.annotate(str(p.get_width()), (p.get_width() * 1.005, p.get_y() * 1.005)) ax.set_yticklabels(ax.get_yticklabels(), rotation=40, ha="right") plt.savefig(chart_title+'.pdf') #fig.autofmt_xdate() plot_graph("top 5 cities with the most records", "Number") transform this program to extract full data right below the graph generated in pdf before save.

from matplotlib.backends.backend_pdf import PdfPages from matplotlib.figure import Figure # Create a PDF document pdf = PdfPages('test.pdf') def plot_graph(chart_title, column_to_label): df=show_data.sort_values(by="Upload", ascending=False)[:5] fig = Figure() ax = fig.add_subplot(111) ax.barh(df.index, df.Upload, align='center') ax.set_yticklabels(df.index) ax.set_yticks(df.index) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel(column_to_label) ax.set_title(chart_title) pdf.savefig(fig) plot_graph("top 5 cities with the most records", "Number") pdf.close()

Questions about programming?Chat with your personal AI assistant