我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用matplotlib.pyplot.FuncFormatter()。
def saveHistogram(fname, x, bins=50): def to_percent(y, pos): s = str(100*y) if plt.rcParams['text.usetex'] is True: return s + r'$\%$' else: return s + '%' # plt.hist(x, bins=bins, normed=True, cumulative=True) plt.hist(x, bins=bins) # formatter = plt.FuncFormatter(to_percent) # plt.gca().yaxis.set_major_formatter(formatter) plt.savefig(fname) plt.close()
def plot3D(data, taxid_list): colors = cm.rainbow(np.linspace(0, 1, len(taxid_list))) fig = plt.figure(figsize=(12,9)) ax = fig.add_subplot(111, projection='3d') fig.suptitle("NCTU receipts") c_index = 0 for taxid in taxid_list: (x, y, z) = data[taxid] print "{} {}".format(taxid, colors[c_index]) #print len(X[taxid]), len(Y[taxid]), len(Z[taxid]) ax.scatter(np.log(x),y,z,c=colors[c_index],marker='o',\ s=20, alpha=0.2, edgecolors='none') #x2, y2, _ = proj3d.proj_transform(x,y,z, ax.get_proj()) #label = plt.annotate(\ #taxid,\ #xy = (x2, y2), xytext = (-20, 20),\ #textcoords = 'offset points', ha = 'right', va = 'bottom',\ #bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.1),\ #arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0')) c_index += 1 ax.set_xlabel('num') #ax.set_xscale('log',nonposx='clip') ax.set_ylabel('med') ax.set_zlabel('ratio') ax.zaxis.set_major_formatter(plt.FuncFormatter(\ lambda x, loc: "{:,}".format(int(x)))) #fig.canvas.mpl_connect('button_release_event', update_position) plt.show()
def plot_hist(self, ax, overlapping=False, formatted_yaxis=True, **kwargs): """Returns a matplotlib style histogram (matplotlib.pyplot.hist) Uses the matplotlib object oriented interface to add a Histogram to an matplotlib Axes object. All named arguments from pyplot.hist can be used. A new argument called "type" makes it possible to make overlapping histogram plots. Args: :ax: (`Axes`) An matplotlib Axes object on which the histogram will be plot :overlapping (`bool`, optional): If set to true, this will generate an overlapping plot. When set to False it will generate a normal grouped histogram. Defaults to False. :formatted_yaxis: (`bool`, optional). If set to true, the numbers on the yaxis will be formatted for better readability. E.g. 1500000 will become 1.5M. Defaults to True :\*\*kwargs: The keyword arguments as used in matplotlib.pyplot.hist """ self.build() if formatted_yaxis: # Round the y-axis value to nearest thousand, million, or billion for readable y-axis formatter = plt.FuncFormatter(Histogram._convert_number_bmk) ax.yaxis.set_major_formatter(formatter) if overlapping: for colname in self.hist_dict: ax.hist(self._get_bin_centers(), bins=self.bin_list, alpha=0.5, label=self.hist_dict.keys(), weights=self.hist_dict[colname], **kwargs ) else: weights_multi = [self.hist_dict[colname] for colname in self.hist_dict] return ax.hist([self._get_bin_centers()] * len(self.hist_dict), bins=self.bin_list, weights=weights_multi, label=self.hist_dict.keys(), **kwargs)
def plot_hist(self, ax, overlapping=False, formatted_yaxis=True, **kwargs): """Returns a matplotlib style histogram (matplotlib.pyplot.hist) Uses the matplotlib object oriented interface to add a Histogram to an matplotlib Axes object. All named arguments from pyplot.hist can be used. A new argument called "type" makes it possible to make overlapping histogram plots. Args: ax (:obj:`Axes`): An matplotlib Axes object on which the histogram will be plot overlapping (:obj:`bool`, optional): If set to true, this will generate an overlapping plot. When set to False it will generate a normal grouped histogram. Defaults to False. formatted_yaxis (:obj:`bool`, optional). If set to true, the numbers on the yaxis will be formatted for better readability. E.g. 1500000 will become 1.5M. Defaults to True **kwargs: The keyword arguments as used in matplotlib.pyplot.hist """ self.build() if formatted_yaxis: # Round the y-axis value to nearest thousand, million, or billion for readable y-axis formatter = plt.FuncFormatter(Histogram._convert_number_bmk) ax.yaxis.set_major_formatter(formatter) if overlapping: for colname in self.hist_dict: ax.hist(self._get_bin_centers(), bins=self.bin_list, alpha=0.5, label=self.hist_dict.keys(), weights=self.hist_dict[colname], **kwargs ) else: weights_multi = [self.hist_dict[colname] for colname in self.hist_dict] return ax.hist([self._get_bin_centers()] * len(self.hist_dict), bins=self.bin_list, weights=weights_multi, label=self.hist_dict.keys(), **kwargs)