【问题标题】:is it possible to combine 2 differents styles in Matplotlib or seaborn in one plot?是否可以在一个情节中结合 Matplotlib 或 seaborn 中的 2 种不同风格?
【发布时间】:2020-07-02 03:00:55
【问题描述】:

我不知道是否可以使用 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))

我得到最终结果

【问题讨论】:

    标签: python matplotlib seaborn mplfinance


    【解决方案1】:

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

    【讨论】:

    • 很高兴。一次还一笔债。
    【解决方案2】:

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

    如需了解更多信息,请参阅:

    注意,作为一般规则,建议不要使用“External Axes Method”,如果您要完成的工作可以 否则在面板模式下使用 mplfinance。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2015-10-09
      • 2012-04-17
      相关资源
      最近更新 更多