如果“意外值”显示,则在“散点图”中标记

时间:2018-05-31 07:55:09

标签: python pandas matplotlib scatter-plot

我有dataframe,就像这样。我想做scatter plots

enter image description here

我想scatter plots Value1 value2但是当scatter plots (Value1)减少到0.6以下时,我想在red color标记为default color,否则{ {1}}没关系。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

添加另一个包含颜色信息的列:

import  matplotlib.cm as cm
df['color'] = [int(value < 0.6) for value in df.Value2]
df.plot.scatter(x=df.index, y='Value1',c='color',cmap=cm.jet)

答案 1 :(得分:1)

我使用了seaborn的lmplot(高级散点图)工具。

您可以在电子表格文件中创建一个名为&#34; Category&#34;的新列。在excel或openoffice中对变量进行分类非常容易

(它是这样的 - &gt;(if(cell_value&lt; 0.6 - &gt; low),if(cell_value&gt; 0.6 - &gt; high))。) < / p>

因此,您的测试数据应如下所示:

spreadsheet

你可以在python中导入数据(我使用带有spider:python 3.6的Anaconda 3.5)我以.txt格式保存了文件。但任何其他格式都是可能的(.csv等)

#Import libraries
import seaborn as sns
import pandas as pd
import numpy as np
import os

#Open data.txt which is stored in a repository
os.chdir(r'C:\Users\DarthVader\Desktop\Graph')
f = open('data.txt')

#Get data in a list splitting by semicolon
data = []
for l in f:
    v = l.strip().split(';')
    data.append(v)
f.close()

#Convert list as dataframe for plot purposes
df = pd.DataFrame(data, columns = ['ID', 'Value', 'Value2','Category'])

#pop out first row with header
df2 = df.iloc[1:]

#Change variables to be plotted as numeric types
df2[['Value','Value2']] = df2[['Value','Value2']].apply(pd.to_numeric)

#Make plot with red color with values below 0.6 and green color with values above 0.6 
sns.lmplot( x="Value", y="Value2", data=df2, fit_reg=False, hue='Category', legend=False, palette=dict(high="#2ecc71", low="#e74c3c")) 

您的输出应如下所示。

Scatterplot