是否可以将Matplotlib或seaborn中的两种不同样式组合在一个绘图中?

2024-09-28 19:01:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

x轴和y轴相同

enter image description here

在下面的响应之后,我选择mplfinance:mplfinance

我有以下数据帧(每日)

enter image description here 通过以下函数,我们可以绘制:

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))

我得到了最后的结果

enter image description here


Tags: truemakestyle绘制seabornmcwidthdict
2条回答

是的,mplfinance允许您在同一绘图或多个子绘图上绘制多个数据集,其中每个数据集可以是烛台、ohlc条形图、直线图、散点图或条形图

有关更多信息,请参见示例:

注意,作为一般规则,建议不要使用“External Axes Method”,如果您试图通过mplfinance在panels模式下完成的任务可以通过其他方式完成

是的,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

您甚至可以使用pandasdf.plot(data,kind='bar',ax=ax,**kwargs)在同一轴对象内进行打印

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

相关问题 更多 >