import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
from pylab import mpl
%config InlineBackend.figure_format = 'svg'
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
x1 = np.arange(, )
x2 = x1[1:]
y1 = np.array([40, 45, 50, 64, 71, 83, 92, 100])
y2 = (y1[1:] - y1[:-1]) / y1[:-1]
fig, ax1 = plt.subplots()
bar = ax1.bar(x1, y1)
ax1.legend(bar, ("销售额",), loc = [0.2, -0.155])
ax2 = ax1.twinx()
def to_percent(value, position):
return "%1.0f" % (100 * value) + '%'
ax2.yaxis.set_major_formatter(FuncFormatter(to_percent))
line = ax2.plot(x2, y2, c='r')
for a, b in zip(x2, y2):
ax2.text(a, b, "%.2f" % (100 * b) + '%', ha='center', va='bottom', fontsize=10)
ax2.legend(line, ("增速",), loc = [0.6, -0.155])
# ax2.spines['top'].set_visible(False) # 隐藏上边框
# ax1.spines['top'].set_visible(False)
ax2.set_title("全球销售额及其增速", fontsize=16)
python双柱状图与双折线图_如何用Python绘制销售额(柱形图)和增速(折线图)的双y轴图?...