1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager import numpy as np from matplotlib.font_manager import FontProperties import matplotlib.ticker as ticker
font_props = font_manager.FontProperties(weight='bold')
plt.rcParams['font.family'] = 'Times New Roman' plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams.update({'font.size': 17}) if __name__ == '__main__': fig = plt.figure(figsize=(6, 5)) fig.set_size_inches(6, 5) ax1 = fig.add_subplot()
x_data = ['1','5','10','15','25','50','100','150','200','250','300'] y_data = [8000000,7000000,6000000,6000000,3000000,1000000,600000,300000,100000,100000,100000] y_data2=[0.8,0.5,0.3,0.8,0.7,0.4,0.7,0.6,0.6,0.6,0.6]
bar=ax1.bar(x_data, y_data, color='royalblue', label="------",zorder=100)
def exp_formatter(x, pos): if x == 0.0: return f'{0}' elif x < 10000000: x = x / 1000000 return f'{x:.0f}e6' else: x = x / 10000000 return f'{x:.0f}e7' ax1.yaxis.set_major_formatter(ticker.FuncFormatter(exp_formatter))
ax1.set_xlabel("XXX", fontsize=20, weight='bold') ax1.set_ylabel("YYY", fontsize=20, weight='bold') ax1.tick_params(axis='y', direction='in',labelsize=18) ax1.tick_params(axis='x', labelsize=18) ax1.set_ylim([0, 10000000]) ax1.yaxis.grid(True, zorder=0)
ax2 = ax1.twinx() line,=ax2.plot(x_data, y_data2, 'gs-', label="......", linewidth=2, markersize=8, markerfacecolor='none', markeredgewidth=2) ax2.set_ylabel("Y222", fontsize=20, weight='bold') ax2.tick_params(axis='y', direction='in',labelsize=18) ax2.set_ylim([0, 1])
legend = [bar, line] labels = [l.get_label() for l in legend] legend=ax1.legend(legend, labels,bbox_to_anchor=(1, 1)) font_props = FontProperties(weight='bold', size=10) for text in legend.get_texts(): text.set_font_properties(font_props)
plt.savefig("pngs/pic3.png", dpi=600, bbox_inches='tight', pad_inches=0)
|