繁体   English   中英

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

[英]Suggestions to plot overlapping lines in matplotlib?

有没有人建议在 plot 上呈现重叠线的最佳方式是什么? 我有很多,我的想法是在不重叠的地方有不同的 colors 的完整线条,在重叠的地方有虚线,这样所有 colors 都可见,并且可以看到重叠的 colors。

但是,我该怎么做。

只需降低线条的不透明度即可使它们透明。 您可以使用alpha变量来实现。 例:

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

其中alpha范围是0-1,其中0是不可见的。

在高度离散的情节中,我也有同样的问题。

这里是起始情况:

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()

没有人能说出绿线和蓝线的确切位置

和我的“解决方案”

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()

感谢您提出改进建议!

假设您的熊猫数据框称为respone_times,那么您可以使用Alpha为图形设置不同的不透明度。 前后检查图片 在此处输入图片说明 使用Alpha。

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

根据您的数据和用例,添加一些随机抖动来人为地分隔线可能没问题。

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

这是来自Markus Dutschke 示例的原始数据图:

原始数据

这是抖动版本, std_ratio设置为0.1

抖动数据

用点或破折号代替实线也可以

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