def draw_graph(dates, counts): ########################################################### # Drawing takes place here. pylab.figure(1) ax = pylab.subplot(111) pylab.plot_date(dates, counts, color='r', linestyle='-', marker='o', markersize=3) ax.xaxis.set_major_formatter(pylab.DateFormatter('%Y')) ax.xaxis.set_major_locator(pylab.YearLocator()) ax.xaxis.set_minor_locator(pylab.MonthLocator()) ax.set_xlim((dates[0] - 92, dates[len(dates) - 1] + 92)) ax.yaxis.set_major_formatter(pylab.FormatStrFormatter('%d')) pylab.ylabel('Total # of Public DAV Servers') lastdate = datetime.datetime.fromordinal(dates[len(dates) - 1]).strftime("%B %Y") pylab.xlabel("Data as of " + lastdate) pylab.title('Security Space Survey of\nPublic Subversion DAV Servers') # End drawing ########################################################### png = open(OUTPUT_FILE, 'w') pylab.savefig(png) png.close() os.rename(OUTPUT_FILE, OUTPUT_FILE + ".tmp.png") try: im = Image.open(OUTPUT_FILE + ".tmp.png", 'r') (width, height) = im.size print("Original size: %d x %d pixels" % (width, height)) scale = float(OUTPUT_IMAGE_WIDTH) / float(width) width = OUTPUT_IMAGE_WIDTH height = int(float(height) * scale) print("Final size: %d x %d pixels" % (width, height)) im = im.resize((width, height), Image.ANTIALIAS) im.save(OUTPUT_FILE, im.format) os.unlink(OUTPUT_FILE + ".tmp.png") except Exception as e: sys.stderr.write("Error attempting to resize the graphic: %s\n" % (str(e))) os.rename(OUTPUT_FILE + ".tmp.png", OUTPUT_FILE) raise pylab.close()
def plot_all_line(self): data = [] label = [] for each_line in Line_list: data.append([ Line_list[each_line].CA_Current_G, Line_list[each_line].CA_Opti_G, Line_list[each_line].CA_Audit_G ]) label.append(Line_list[each_line].name) pylab.xlabel("Lignes") pylab.ylabel("Chiffre d'Affaires") pylab.title("Chiffre d'affaires par ligne") dim = len(data[0]) w = 0.75 dimw = w / dim color = ['r', 'g', 'y'] titre = ['CA actuelle', 'CA optimal', 'CA audit'] x = pylab.arange(len(data)) for i in range(len(data[0])): y = [d[i] for d in data] pylab.bar(x + i * dimw, y, dimw, bottom=0.001, color=color[i % 3], label=titre[i % 3]) pylab.legend() pylab.gca().set_xticks(x + w / 2) pylab.gca().set_xticklabels(label, rotation=90) pylab.subplots_adjust(bottom=0.1, top=0.96, right=0.98, left=0.05) major_formatter = pylab.FormatStrFormatter('%2.0f') pylab.gca().yaxis.set_major_formatter(major_formatter) pylab.get_current_fig_manager().window.showMaximized() pylab.show()
import numpy as np import torch from matplotlib import pylab from torch.autograd import Variable data = Variable( torch.FloatTensor([[0.1, 0.2, 0.4, 0.3, 0.1], [0.05, 0.09, 0.8, 0.1, 0.02], [0.01, 0.9, 0.04, 0.08, 0.07]])) print(data) x = np.arange(5) x = np.tile(x, (3, 1)) #print(x) group_labels = ['type 1', 'type 2', 'type 3', 'type 4', 'type 5'] ymajorLocator = pylab.MultipleLocator(0.1) #将y的刻度设置为0.1的倍数 ymajorFormatter = pylab.FormatStrFormatter('%1.1f') #设置y轴标签文本的格式 yminorLocator = pylab.MultipleLocator(0.01) #将此y轴次刻度标签设置为0.1的倍数 ax = plt.subplot(111) ax.yaxis.set_major_locator(ymajorLocator) ax.yaxis.set_major_formatter(ymajorFormatter) ax.yaxis.set_minor_locator(yminorLocator) for i in range(x.shape[0]): ax.plot(x[i], data.data.numpy()[i], label='sample ' + str(i)) ax.set_xticks(x[0]) ax.set_xticklabels(group_labels, rotation=0) ax.legend() ax.grid() plt.show() ax = plt.subplot(111)