首页 > 解决方案 > 自定义虚线 matplotlib

问题描述

我正在比较两个图,为了区分它们,我将一个设为实线,另一个设为虚线。但是我可以看到差异,但不清楚。
在此处输入图像描述

有没有办法只自定义虚线图,以便我可以清楚地看到差异?我在图中尝试了破折号(5,5),但它也适用于实线。

编辑:这是我的代码的一部分

def plot(self):
        # if self.image_count == 2:
        #     self.image_count = 0
        print("plot")
        self.counter=0
        if self.widget:
            self.widget.destroy()
        if self.toolbar:
            self.toolbar.destroy()
        fig = plt.figure()
        marker_list = ['-','--','-.',':']
        plot1 = fig.add_subplot(111)

        #setting x and y label of graph
        plot1.set_xlabel('Time (sec)')
        
        # plot1.set_ylabel('Y - axis')

        #creating frame for plotting graph
        graph_frame = Frame(self.window)
        graph_frame.place(relx=0.01, rely=0.1, relwidth=0.98, relheight=0.88)

        cs = self.list.curselection()
        for index in cs:
            f_name = self.list.get(index).split(":")[0]
            c_name = self.list.get(index).split(":")[1]
            x = self.cev_files[f_name]['Atd[0].Time'].tolist()
            y = self.cev_files[f_name][c_name].tolist()
            plot1.plot(x, y, linestyle=marker_list[self.counter],linewidth=1, label=c_name)#linewidth=1, markersize=12,
            plot1.set_ylabel(c_name)
            self.counter +=1
            if self.counter==3:
                self.counter=0
            # handles, labels = plot1.get_legend_handles_labels()
            # lgd = plot1.legend(handles, labels, bbox_to_anchor=(0.9, 0.9))
            # fig.tight_layout()
            # self.image_count +=1
        #always place on upper right
        plot1.legend(loc="upper right")
        
        # plot1.legend(loc='best')
        # plot1.grid(linestyle ='--',linewidth=0.4)
        #place on best position
        #plot1.legend()
        if self.flag==True:
            img_name = str(time.time())+'.png'
            fig.savefig(img_name,  bbox_inches='tight')
            self.images.append(img_name)
        # creating the Tkinter canvas
        # containing the Matplotlib figure
        
        canvas = FigureCanvasTkAgg(fig,
                                   master=graph_frame)
        canvas.draw()

PS:我只能使用这种颜色。

标签: pythonmatplotlib

解决方案


当您想要在绘图之间有所不同时,线条样式总是更好。您可以使用任何其他样式(不仅仅是虚线)来获得更清晰和不同的情节。对于样式检查here

根据我的经验,增加 matplotlib 图形的大小将突出显示情节的一些小特征(线条样式等)。你可以尝试增加情节的大小。

from matplotlib.pyplot import figure

figure(figsize=(8, 6), dpi=80)

推荐阅读