200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python双柱状图与双折线图_如何用Python绘制销售额(柱形图)和增速(折线图)的双y轴

python双柱状图与双折线图_如何用Python绘制销售额(柱形图)和增速(折线图)的双y轴

时间:2022-03-07 22:11:03

相关推荐

python双柱状图与双折线图_如何用Python绘制销售额(柱形图)和增速(折线图)的双y轴

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轴图?...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。