user96564
user96564

Reputation: 1607

Marked in Scatter plots, if Unexpected Values shows

I have a dataframe, like this. I want to do scatter plots of it.

enter image description here

I want to do scatter plots of Value1 but whenever value2 is decreased to below 0.6, I want to marked in those scatter plots (Value1) to red color otherwise default color is okay.

Any Suggestions ?

Upvotes: 0

Views: 96

Answers (2)

Ilya
Ilya

Reputation: 193

Add another column with color information:

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)

Upvotes: 1

Andy McRae
Andy McRae

Reputation: 665

I use seaborn's lmplot (advanced scatterplot) tool for that.

You can make a new column in your spreadsheet file with name "Category". It's very easy to categorize variables in excel or openoffice

(It's something like this -> (if(cell_value<0.6-->low),if(cell_value>0.6-->high)).)

So your test data should look like this:

spreadsheet

Than you can import the data in python (I use Anaconda 3.5 with spider: python 3.6) I saved the file in .txt format. but any other format is possible (.csv etc.)

#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")) 

Your output should look like this.

Scatterplot

Upvotes: 1

Related Questions