Skip to main content
 首页 » 编程设计

python之有没有一种方法可以将带有条形图的 3 条线图绘制为具有不同 y 轴的一个图形

2024年11月01日9JustinYoung

我想在 python pandas 中将 3 个线图与条形图一起绘制为一个图形。如何将这些与不同的 y 轴一起绘制?例如,一条线图作为条形图的目标线,另一条线图作为另一条线图的目标。

所以任务是将 4 个图绘制为一个图,我有 2 个不同的变量,一个是蛋白质百分比,它是一个条形图,另一个是卡路里,它是一个线图,它们共享相同的 x 轴是几个月。现在我想包括其他 2 个线图,每个线图都作为上面两个定义变量的目标变量。两个线图必须共享一个 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() 

结果图:

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