简体   繁体   English

给matplotlib中的plot重叠线的建议?

[英]Suggestions to plot overlapping lines in matplotlib?

Does anybody have a suggestion on what's the best way to present overlapping lines on a plot?有没有人建议在 plot 上呈现重叠线的最佳方式是什么? I have a lot of them, and I had the idea of having full lines of different colors where they don't overlap, and having dashed lines where they do overlap so that all colors are visible and overlapping colors are seen.我有很多,我的想法是在不重叠的地方有不同的 colors 的完整线条,在重叠的地方有虚线,这样所有 colors 都可见,并且可以看到重叠的 colors。

But still, how do I that.但是,我该怎么做。

Just decrease the opacity of the lines so that they are see-through. 只需降低线条的不透明度即可使它们透明。 You can achieve that using the alpha variable. 您可以使用alpha变量来实现。 Example: 例:

plt.plot(x, y, alpha=0.7)

Where alpha ranging from 0-1, with 0 being invisible. 其中alpha范围是0-1,其中0是不可见的。

I have the same issue on a plot with a high degree of discretization. 在高度离散的情节中,我也有同样的问题。

Here the starting situation: 这里是起始情况:

import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
        [1,1,1,4,4,4,3,5,6,0],
        [1,1,1,5,5,5,3,5,6,0],
        [1,1,1,0,0,3,3,2,4,0],
        [1,2,4,4,3,2,3,2,4,0],
        [1,2,3,3,4,4,3,2,6,0],
        [1,1,3,3,0,3,3,5,4,3],
        ]

for gg,graph in enumerate(graphs):
    plt.plot(grid,graph,label='g'+str(gg))

plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()

No one can say where the green and blue lines run exactly 没有人能说出绿线和蓝线的确切位置

and my "solution" 和我的“解决方案”

import matplotlib.pyplot as plt
grid=[x for x in range(10)]
graphs=[
        [1,1,1,4,4,4,3,5,6,0],
        [1,1,1,5,5,5,3,5,6,0],
        [1,1,1,0,0,3,3,2,4,0],
        [1,2,4,4,3,2,3,2,4,0],
        [1,2,3,3,4,4,3,2,6,0],
        [1,1,3,3,0,3,3,5,4,3],
        ]

for gg,graph in enumerate(graphs):
    lw=10-8*gg/len(graphs)
    ls=['-','--','-.',':'][gg%4]
    plt.plot(grid,graph,label='g'+str(gg), linestyle=ls, linewidth=lw)

plt.legend(loc=3,bbox_to_anchor=(1,0))
plt.show()

I am grateful for suggestions on improvement! 感谢您提出改进建议!

imagine your panda data frame is called respone_times, then you can use alpha to set different opacity for your graphs. 假设您的熊猫数据框称为respone_times,那么您可以使用Alpha为图形设置不同的不透明度。 Check the picture before and after 前后检查图片 在此处输入图片说明 using alpha. 使用Alpha。

plt.figure(figsize=(15, 7))
plt.plot(respone_times,alpha=0.5)
plt.title('a sample title')
plt.grid(True)
plt.show()

Depending on your data and use case, it might be OK to add a bit of random jitter to artificially separate the lines.根据您的数据和用例,添加一些随机抖动来人为地分隔线可能没问题。

from numpy.random import default_rng
import pandas as pd

rng = default_rng()

def jitter_df(df: pd.DataFrame, std_ratio: float) -> pd.DataFrame:
    """
    Add jitter to a DataFrame.
    
    Adds normal distributed jitter with mean 0 to each of the
    DataFrame's columns. The jitter's std is the column's std times
    `std_ratio`.
    
    Returns the jittered DataFrame.
    """
    std = df.std().values * std_ratio
    jitter = pd.DataFrame(
        std * rng.standard_normal(df.shape),
        index=df.index,
        columns=df.columns,    
    )
    return df + jitter

Here's a plot of the original data from Markus Dutschke's example :这是来自Markus Dutschke 示例的原始数据图:

原始数据

And here's the jittered version, with std_ratio set to 0.1 :这是抖动版本, std_ratio设置为0.1

抖动数据

Replacing solid lines by dots or dashes works too用点或破折号代替实线也可以

g = sns.FacetGrid(data, col='config', row='outputs', sharex=False)
g.map_dataframe(sns.lineplot, x='lag',y='correlation',hue='card', linestyle='dotted')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM