【问题标题】:how to make line plots with foreground and background?如何制作具有前景和背景的线图?
【发布时间】:2020-11-23 22:05:35
【问题描述】:

到底如何在一个绘图中制作多条线的线图,但每条线都设置在背景中,几乎就像是 3D 一样?

参见下面的示例图片:

【问题讨论】:

    标签: python r matplotlib ggplot2


    【解决方案1】:

    在 R 中,您可以像这样接近:

    library(ggridges)
    
    set.seed(69)
    
    df <- data.frame(x = as.vector(sapply(14:10, function(i) rnorm(30, i, 2))),
                     group = rep(letters[1:5], each = 30))
    
    ggplot(df, aes(x, y = group, fill = group)) + 
      geom_vline(aes(xintercept = 10), size = 2, color = "#5078be") +
      geom_density_ridges(size = 2, aes(color = group)) +
      geom_vline(aes(xintercept = 3), size = 2) +
      scale_fill_manual(values = c("#8f4b4a", "#c08f33", "#e2baba", "#ffe2ae", "#83a8f1")) +
      scale_colour_manual(values = c("#5c1a08", "#8c5b01", "#af8987", "#d2ab83", "#5078be")) +
      theme_void() +
      theme(legend.position = "none")
    

    【讨论】:

      【解决方案2】:

      如果您想在R 中绘制这种图(山脊线图),请查看ggridges 包。这将向您展示一堆很酷的示例:

      browseVignettes("ggridges")
      

      【讨论】:

        【解决方案3】:

        您可以使用 z 轴来做到这一点。您仍然可以根据需要调整设计和隐藏轴等。

        from scipy import exp
        import matplotlib.pyplot as plt
        import numpy as np
        from mpl_toolkits import mplot3d
        
        def gaus(x, a, x0, sigma):
            return a*exp(-(x-x0)**2/(2*sigma**2))
        
        if __name__ == '__main__':
            x = np.array([i for i in range(0, 100)])
            y1 = gaus(x, 1, 50, 5)
            y2 = gaus(x, 1, 45, 12)
        
            fig = plt.figure()
            ax = plt.axes(projection='3d')
            ax.view_init(-90, 90)
        
            ax.plot3D(x, y1, 100)
            ax.plot3D(x, y2, 1)
            plt.show()
        

        【讨论】:

          猜你喜欢
          • 2011-05-05
          • 2011-03-14
          • 2023-04-11
          • 2022-06-10
          • 1970-01-01
          • 2011-03-13
          • 2012-07-07
          • 1970-01-01
          • 2016-06-13
          相关资源
          最近更新 更多