我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用matplotlib.pyplot.twinx()。
def plot_res(test_err, train_batch_loss, benchmark_err, epoch): flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"] test_x_val = np.array(list(x * 3 for x in range(0, len(test_err)))) plt.plot(train_batch_loss[0],train_batch_loss[1], label="Training error", c=flatui[1], alpha=0.5) plt.plot(test_x_val, np.array(test_err), label="Test error", c=flatui[0]) plt.axhline(y=benchmark_err[1], linestyle='dashed', label="No-modell error", c=flatui[2]) plt.axhline(y=0.098, linestyle='dashed', label="State of the art error", c=flatui[3]) plt.suptitle("Model error - cold queries") plt.yscale('log', nonposy='clip') plt.xlim([0,epoch+1]) # second_axes = plt.twinx() # create the second axes, sharing x-axis # second_axes.set_yticks([0.2,0.4]) # list of your y values plt.xlabel('epoch') plt.ylabel('error') plt.legend(loc='upper right') plt.show()
def plot_history(logger, title): df = pd.DataFrame(logger.history) df[['acc', 'val_acc']].plot() plt.ylabel("accuracy") df[['loss', 'val_loss']].plot(linestyle='--', ax=plt.twinx()) plt.ylabel("loss") plt.title(title) plt.show()
def plot_history(logger): df = pd.DataFrame(logger.history) df[['acc', 'val_acc']].plot() plt.ylabel("accuracy") df[['loss', 'val_loss']].plot(linestyle='--', ax=plt.twinx()) plt.ylabel("loss")
def freeze_color_cycle(ax): """A function that freezes the color cycle. This is useful for example when the twinx() command is used and the color cycle would normally be reseted. Usage: import matplotlib.pyplot as plt import numpy as np plt.close('all') for i in range(3): plt.plot(np.random.random(20)) ax=plt.gca() cc=freeze_color_cycle(ax) plt.twinx() ax=plt.gca() ax.set_color_cycle(cc) #plot some more on the new twined axes for i in range(3): plt.plot(np.random.random(20)) #When we set-up a new figure we start with blue again fig=figure() for i in range(3): plot(np.random.random(20)) """ import matplotlib as mpl if mpl.__version__ >= '1.5.1': next_color=ax._get_lines.prop_cycler.next()['color'] else: next_color=next(ax._get_lines.color_cycle) ix=plt.rcParams['axes.color_cycle'].index(next_color) color_cycle=plt.rcParams['axes.color_cycle'][ix:]+plt.rcParams['axes.color_cycle'][:ix] return color_cycle
def draw_timeseries(packets_all_hosts, cutoff_time_ms, save_dir, output_postfix): """ Draw a timeseries of packet latencies over time, in order that the packets were sent in """ plt.figure() plt.suptitle("Packet latency over time") n_hosts = len(packets_all_hosts) for board_n in range(n_hosts): (filename, packet_ns, latencies_ms, total_n_packets) = packets_all_hosts[board_n] # Make copies, so that we don't distort the data that the other plots make use of packet_ns = list(packet_ns) latencies_ms = list(latencies_ms) (packet_ns, latencies_ms, dropped_packet_nos) = \ add_dropped_packets_and_sort(total_n_packets, packet_ns, latencies_ms) plt.subplot(n_hosts, 1, 1 + board_n) plt.title(filename) line = plt.plot(packet_ns, latencies_ms)[0] if board_n == (n_hosts - 1): plt.xlabel("Packet no.") plt.ylabel("Latency (ms)") plt.twinx() # Set up a second y-axis (bin_starts, bin_width_packets, drops) = drops_or_delays_in_each_bin( packet_ns, latencies_ms, cutoff_time_ms) bars = plt.bar(bin_starts, drops, bin_width_packets, alpha=0.5, color='red') plt.ylabel("Pct. packets dropped\nor > %d ms" % cutoff_time_ms) plt.ylim([0, 100]) if board_n == 0: plt.legend([line, bars], ['Latencies', 'Pct. delayed packets']) plt.tight_layout() plt.subplots_adjust(top=0.9) # to make room for suptitle plot_filename = os.path.join(save_dir, 'udp_latency_timeseries%s.png' % output_postfix) plt.savefig(plot_filename)