【问题标题】:How to connect subplots with a dotted line boundary instead of a solid line with Matplotlib?如何将子图与虚线边界而不是实线与 Matplotlib 连接?
【发布时间】:2021-04-20 01:22:12
【问题描述】:

如何在python中用虚线而不是实线连接子图?我使用此站点https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html 水平组合了 3 个散点图,但它们之间有一条实线。有没有办法用虚线加入它们,所以它看起来像一个时间线,something like shown in this image

【问题讨论】:

    标签: python matplotlib scatter-plot subplot dotted-line


    【解决方案1】:

    这可以通过自定义官方参考中的示例并隐藏图框的左右两侧来实现。 这个例子只隐藏了框架,所以刻度线仍然存在,看起来像一条虚线。如果您对此感到满意,则可以按原样使用它。如果你想自定义它,你需要隐藏刻度并添加带有ax.vlines或类似的虚线。

    import matplotlib.pyplot as plt
    import numpy as np
    
    # Some example data to display
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)
    
    fig = plt.figure()
    gs = fig.add_gridspec(1,3, wspace=0)
    axs = gs.subplots(sharex=True, sharey=True)
    fig.suptitle('Sharing both axes')
    axs[0].plot(x, y ** 2)
    axs[1].plot(x, 0.3 * y, 'o')
    axs[2].plot(x, y, '+')
    axs[0].spines['right'].set_visible(False)
    axs[1].spines['left'].set_visible(False)
    axs[1].spines['right'].set_visible(False)
    axs[1].spines['left'].set_visible(False)
    axs[2].spines['left'].set_visible(False)
    
    # Hide x labels and tick labels for all but bottom plot.
    for ax in axs:
        ax.label_outer()
    

    【讨论】:

    • axs[0].spines['right'].set_visible(False) 谢谢这是我正在寻找但找不到的命令;这确实有助于解决我的问题
    【解决方案2】:

    您的问题不是很清楚,但我认为您需要一种方法在情节中绘制vlines。 这可能会有所帮助。

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Some example data to display
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)
    
    fig, ax = plt.subplots()
    ax.plot(x, y,'b--')
    ax.vlines(x =[1,2,3,4,5,6], ymin=-1.085, ymax=1.085, colors='k', linestyles='--')
    ax.grid()
    

    或者可能是这样的:

    import numpy as np 
    import matplotlib.pyplot as plt
    
    x = [1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1]
    t = np.arange(0,len(x))
    
    code_1 = (1,0,1,0,1,0)
    code_0 = (0,1,0,1,0,1)
    x1 = [1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0]
    
    x2 = np.logical_xor(x1, x)
    
    g, (ax0, ax1, ax2) = plt.subplots(3, sharex=True, sharey=True, figsize=(12,8))
    
    ax0.step(t,x,'b', lw=3, label = "Data Signal");ax0.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax0.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.085, colors='k', linestyles='--');ax0.axes.xaxis.set_visible(False);ax0.axes.yaxis.set_visible(False);plt.grid(True)
    
    arrow_kwargs = {'arrowprops':dict(arrowstyle="<->",lw=2.5)}
    ax0.annotate(xytext=(5,1.1), xy=(11,1.1), s='', **arrow_kwargs); ax0.text(8, 1.2, '$T_{b} $', va='bottom', ha='center',fontsize=20)
    
    ax1.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.1, colors='k', linestyles='--');ax1.step(t,x1, 'm', lw=3, label = "Pseudo-random\ncode",alpha = 0.6);ax1.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax1.axes.xaxis.set_visible(False);ax1.axes.yaxis.set_visible(False);ax1.grid()
    ax2.step(t,x2, 'orange', lw=3, label = "Transmitted signal\nData XOR PN code",alpha = 0.6);ax2.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=16);ax2.axes.xaxis.set_visible(False);ax2.axes.yaxis.set_visible(False);ax2.grid()
    ax2.vlines(x =[np.arange(0,len(x))], ymin=-0.1, ymax=1.1, colors='k', linestyles='--')
    ax2.annotate(xytext=(8,0.1), xy=(9,0.1), s='', **arrow_kwargs); ax2.text(8.5, 0.15, '$T_{c}$', va='bottom', ha='center',fontsize=20)
    g.subplots_adjust(hspace=0.1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 2015-05-23
      • 2015-01-14
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多