是否可以在一个图中结合 Matplotlib 或 seaborn 中的 2 种不同样式?

is it possible to combine 2 differents styles in Matplotlib or seaborn in one plot?

我不知道是否可以使用 Matplotlib 或 seaborn 或其他工具在一个图中绘制 1 条线和 1 条柱(烛台样式)。喜欢下图(在excel):

x轴和y轴相同

根据以下回复,我选择 mplfinance:mplfinance

我有以下数据框(每天)

并使用以下函数我们可以绘制:

def ploting_chart(daily):
    # Take marketcolors from 'yahoo'
    mc = mpf.make_marketcolors(base_mpf_style='yahoo',up='#ff3300',down='#009900',inherit=True)

    # Create a style based on `seaborn` using those market colors:
    s  = mpf.make_mpf_style(base_mpl_style='seaborn',marketcolors=mc,y_on_right=True,
    gridstyle = 'solid' , mavcolors = ['#4d79ff','#d24dff']
    )

    # **kwargs
    kwargs = dict(
        type='candle',mav=(7,15),volume=True, figratio=(11,8),figscale=2,
        title = 'Covid-19 Madagascar en traitement',ylabel = 'Total en traitement',
        update_width_config=dict(candle_linewidth=0.5,candle_width=0.5),
        ylabel_lower = 'Total'
        )

    # Plot my new custom mpf style:
    mpf.plot(daily,**kwargs,style=s,scale_width_adjustment=dict(volume=0.4))

我得到了最终结果

是的,plt.figureplt.subplots 给你一个图形对象,然后你可以绘制任意数量的图形。事实上,如果你使用

import seaborn as sns
fmri = sns.load_dataset("fmri")

f,ax = plt.subplots(1,1,figsize=(10,7)) # make a subplot of 1 row and 1 column


g1 = sns.lineplot(x="timepoint", y="signal", data=fmri,ax=ax) # ax=axis object is must
g2 = sns.some_other_chart(your_data, ax=ax)
g3 = ax.some_matlotlib_chart(your_data) # no need to use ax=ax

Seaborn 不支持 Candlestick 但您可以在同一轴上使用 matplotlib 进行绘图。

from matplotlib.finance import candlestick_ohlc
candlestick_ohlc(ax, data.values, width=0.6, colorup='g', colordown='r') # just a dummy code to explain. YOu can see the ax object here as first arg

您甚至可以使用 pandas df.plot(data,kind='bar',ax=ax,**kwargs) 在同一轴对象内绘图。

注意:某些seaborn图表不支持在同一ax上绘图,因为它们使用自己的grid,例如relplot

是的,mplfinance 允许您在同一个图或多个子图中绘制多个数据集,其中每个图都可以是烛台、ohlc-bar、折线图、散点图或条形图中的任何一个.

有关详细信息,请参阅示例:

注意,一般来说,建议不要使用“External Axes Method”如果你是尝试完成 可以 以其他方式在面板模式下使用 mplfinance 完成。