Если вы не против перейти на matplotlib для этого:
import matplotlib.pyplot as plt
xs = [1, 2, 3, 4, 5] # x values
bar_ys = [28, 20, 10, 40, 10] # left y axis
line_ys = [10, 20, 35, 40, 60] # right y axis
fig, bar_ax = plt.subplots()
bar_ax.bar(xs, bar_ys, color='blue') # plot first y series (line)
bar_ax.set_xlabel('x values') # label for x axis
bar_ax.set_ylabel('bar values') # label for left y axis
bar_ax.tick_params('y', colors='blue') # add color to left y axis
line_ax = bar_ax.twinx()
line_ax.plot(xs, line_ys, color='red') # plot second y series (bar)
line_ax.set_ylabel('line values') # label for right y axis
line_ax.tick_params('y', colors='red') # add color to right y axis
plt.show()
Результирующая цифра:
Кпостроить несколько линейных графиков, просто добавьте новые вызовы к line_ax.plot
, передавая новые значения Y в каждом вызове:
import matplotlib.pyplot as plt
xs = [1, 2, 3, 4, 5] # x values
bar_ys = [28, 20, 10, 40, 10] # left y axis
line_ys_1 = [10, 20, 35, 40, 60] # right y axis
line_ys_2 = [40, 40, 40, 40, 40] # right y axis
line_ys_3 = [10, 20, 10, 20, 60] # right y axis
fig, bar_ax = plt.subplots()
bar_ax.bar(xs, bar_ys, color='blue') # plot first y series (line)
bar_ax.set_xlabel('x values') # label for x axis
bar_ax.set_ylabel('bar values') # label for left y axis
bar_ax.tick_params('y', colors='blue') # add color to left y axis
line_ax = bar_ax.twinx()
line_ax.plot(xs, line_ys_1, color='red') # plot second y series (bar)
line_ax.plot(xs, line_ys_2, color='green') # plot second y series (bar)
line_ax.plot(xs, line_ys_3, color='yellow') # plot second y series (bar)
line_ax.set_ylabel('line values') # label for right y axis
plt.show()
Результирующая цифра:
По сути, вы можете использовать любую комбинацию вызовов для ax.plot
, ax.bar
и любой другой тип графика для осей, которые вы получаете от plt.subplots
и ax.twinx
. Графики, вызываемые на одном экземпляре топора, будут иметь одинаковую ось Y.