【问题标题】:Define colour and marker in matplotlib using two different columns使用两个不同的列在 matplotlib 中定义颜色和标记
【发布时间】:2022-01-06 16:02:02
【问题描述】:

我正在使用 matplotlib 创建散点图。

我在我的数据框中使用两个不同的列来定义每个参数的两个参数,它们是发现样本的站点位置和地下深度。

我想格式化散点图,以便站点位置是特定颜色,然后样本深度是不同的标记类型。目前我只是设法按颜色分开:

df = pd.DataFrame({"Sites" : ["Site 1","Site 1", "Site 1", "Site 1", "Site 3", "Site 3"],
               "Depth" : ["a" , "b", "c", "d", "e", "f"],
               "D/L_FAA" : [0.1 , 0.2, 0.3, 0.4, 0.5, 0.6],
               "D/L_THAA" : [0.1 , 0.2, 0.3, 0.4, 0.5, 0.6]})
df
Out[8]: 
    Sites Depth  D/L_FAA  D/L_THAA
0  Site 1     a      0.1       0.1
1  Site 1     b      0.2       0.2
2  Site 1     c      0.3       0.3
3  Site 1     d      0.4       0.4
4  Site 3     e      0.5       0.5
5  Site 3     f      0.6       0.6

ax = plt.figure(figsize=(8, 6))
groups = df.groupby(['Sites'])
for name, group in groups:
    plt.plot(group["D/L_FAA"], group["D/L_FAA"], marker='o', linestyle='', markersize=5, label=name)

Just colours

这就是我想要实现的目标:

enter image description here

感谢您的帮助。

【问题讨论】:

    标签: python matplotlib formatting data-visualization scatter


    【解决方案1】:

    使用 seaborn 的 sns.scatterplot 只需几行代码即可完成。

    见下面的代码:

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = pd.DataFrame({"Sites" : ["Site 1","Site 1", "Site 1", "Site 1", "Site 3", "Site 3"],
                   "Depth" : ["a" , "b", "c", "d", "e", "f"],
                   "D/L_FAA" : [0.1 , 0.2, 0.3, 0.4, 0.5, 0.6],
                   "D/L_THAA" : [0.1 , 0.2, 0.3, 0.4, 0.5, 0.6]})
    sns.scatterplot(data=df, x='D/L_FAA',y='D/L_THAA',hue='Sites',style='Depth')
    plt.show()
    

    输出给出:

    【讨论】:

    • 谢谢。有没有办法让标记和颜色在图例上不分开?
    • 我已经更新了我想要实现的图像以使其更清晰
    • 签出this
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 2018-10-06
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多