Two color scatter plots in R or in python

I have a dataset of three columns and n number of rows. column 1 contains the name, value of column 2, and the value of column 3 (rank2).

I want to plot a scatter plot with outlier values ​​displaying names.

The commands RI use are as follows:

tiff('scatterplot.tiff')
data<-read.table("scatterplot_data", header=T)
attach(data)
reg1<-lm(A~B)
plot(A,B,col="red")
abline(reg1)
outliers<-data[which(2^(data[,2]-data[,3]) >= 4 | 2^(data[,2]-data[,3]) <=0.25),]

text(outliers[,2], outliers[,3],labels=outliers[,1],cex=0.50)

dev.off()

and I get this figure: enter image description here

I want the labels on the bottom half to be the same color, and the labels in the top half to have a different color, like green and red.

Any suggestions or settings in the teams?

+3
source share
2 answers

You already have a logic test that is suitable for your satisfaction. Just use it in the color specification for text:

     text(outliers[,2], outliers[,3],labels=outliers[,1],cex=0.50, 
         col=c("blue", "green")[ 
                which(2^(data[,2]-data[,3]) >= 4 ,  2^(data[,2]-data[,3]) <=0.25)] )

, , , , which() 1 >= 4 2 <= 0.25 integer (0) "".

+5

python, matplotlib (pylab) scipy, numpy . numpy , .

. ? :

import scipy as sci
import numpy as np
import pylab as plt

# Create some data
N = 1000
X = np.random.normal(5,1,size=N)
Y = X + np.random.normal(0,5.5,size=N)/np.random.normal(5,.1)
NAMES = ["foo"]*1000 # Customize names here

# Fit a polynomial
(a,b)=sci.polyfit(X,Y,1)

# Find all points above the line
idx = (X*a + b) < Y

# Scatter according to that index
plt.scatter(X[idx],Y[idx], color='r')
plt.scatter(X[~idx],Y[~idx], color='g')

# Find top 10 outliers
err = ((X*a+b) - Y) ** 2
idx_L = np.argsort(err)[-10:]
for i in idx_L:
    plt.text(X[i], Y[i], NAMES[i])

# Color the outliers purple or black
top = idx_L[idx[idx_L]]
bot = idx_L[~idx[idx_L]]

plt.scatter(X[top],Y[top], color='purple')
plt.scatter(X[bot],Y[bot], color='black')

XF = np.linspace(0,10,1000)
plt.plot(XF, XF*a + b, 'k--') 
plt.axis('tight')
plt.show()

enter image description here

+4

All Articles