Python 有没有一种方法可以用条形图将3条直线图绘制为一个具有不同y轴的图形?

Python 有没有一种方法可以用条形图将3条直线图绘制为一个具有不同y轴的图形?,python,matplotlib,bokeh,Python,Matplotlib,Bokeh,我想用一个柱状图在python中将3个线图绘制为一个图形。如何用不同的y轴将它们绘制在一起?例如,一个线形图作为条形图的目标线,另一个线形图作为其他线形图的目标线 所以我们的任务是把4个图绘制成一个单一的图表,我有两个不同的变量,一个是蛋白质百分比,它是一个条形图,另一个是卡路里,它是一个直线图,它们都有相同的x轴,也就是月份。现在我想包括另外两个线条图,每个线条图都是上面两个定义变量的目标变量。两个线形图必须共享一个y轴,另一个线形图必须与条形图共享另一个y轴。我设法用不同的y轴将我的两个变量

我想用一个柱状图在python中将3个线图绘制为一个图形。如何用不同的y轴将它们绘制在一起?例如,一个线形图作为条形图的目标线,另一个线形图作为其他线形图的目标线

所以我们的任务是把4个图绘制成一个单一的图表,我有两个不同的变量,一个是蛋白质百分比,它是一个条形图,另一个是卡路里,它是一个直线图,它们都有相同的x轴,也就是月份。现在我想包括另外两个线条图,每个线条图都是上面两个定义变量的目标变量。两个线形图必须共享一个y轴,另一个线形图必须与条形图共享另一个y轴。我设法用不同的y轴将我的两个变量绘制为一个图形,也设法用一条线绘制一个y轴与一个变量共享,但现在我无法用另一条线绘制另一个y轴与另一个变量共享

from bokeh.palettes import PuBu
from bokeh.io import show, output_notebook
from bokeh.models import ColumnDataSource, ranges, LabelSet, HoverTool
from bokeh.plotting import figure, show
from bokeh.models import LinearAxis, Range1d
# My word count data
months = nutrients_group.Month.tolist() 
p_percentage = nutrients_group.Protein_percetange.tolist()
calories = nutrients_group.Energy.tolist()
#protein_percent = [round(x) for x in p_percentage]
source = ColumnDataSource(dict(x=months, y=p_percentage))

# Output the visualization directly in the notebook
#output_notebook()

# Create a figure with a datetime type x-axis
fig = figure(title='Year overview with target',
             plot_height=400, plot_width=700,
             x_axis_label='Months', y_axis_label='Calories',
             x_minor_ticks=2,
             toolbar_location=None)







#labels = LabelSet(x=months, y=p_percentage)
fig.extra_y_ranges = {"Protein": Range1d(start=0, end=100)}
fig.add_layout(LinearAxis(y_range_name="Protein", axis_label='Protein %'), 'right')
fig.vbar(x='x',  top='y', 
         color='#27c738', width=0.75, 
         legend='Protein %', y_range_name= "Protein", source=source)

fig.line(x=months, y=130000, 
         color='purple', line_width=2,
         legend='Target 30%')
fig.line(x=months, y=240000, 
         color='red', line_width=2,
         legend='Calories Target 240000')

fig.line(x=months, y=calories, 
         color='#00b7f0', line_width=1,
         legend='Calories')
fig.y_range = Range1d(0, 400000)



fig.legend.location = 'top_right'

hover = HoverTool(
    tooltips=[
        ("month", "@x"),
        ("value", "@y")
    ]
)

# Let's check it out
fig.add_tools(hover)
show(fig)[enter image description here][1]

我希望我的目标线图变量共享两个不同的y轴。

如果您不介意切换到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()
结果数字:


基本上,您可以对从
plt.subplot
ax.twinx
中获得的轴使用调用
ax.plot
ax.bar
和任何其他打印类型的任意组合。在同一个ax实例上调用的绘图将共享同一个Y轴。

非常感谢,它帮助了我很多,我真的很努力!