Reputation: 7273
Here is my dataset: t.csv
I am looking for a display like this:
The red dot for negative value, grey for 0, and blue for positive.
I tried to refer the example from the : Logistic Regression
def plot_decision_boundary(pred_func):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
Got the above function for the plotting but didn't understood it.
I want to have the look as above image. How I can do it? Please let me know.
Upvotes: 0
Views: 53
Reputation: 5686
Okay, let's start by loading your data
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_csv('/home/b2003864/t.csv', header=0)
Next it's easy enough to get the scatter points you desire using ax.scatter
fig, ax = plt.subplots(1, 1)
# Plot positive values in red (C3)
ax.scatter(np.argwhere(df['real'] > 0), df.loc[df['real'] > 0, 'real'], color='C3', edgecolors='k')
# Plot negative values in blue (C0)
ax.scatter(np.argwhere(df['real'] < 0), df.loc[df['real'] < 0, 'real'], color='C0', edgecolors='k')
# Plot neutral value in grey (C7)
ax.scatter(np.argwhere(df['real'] == 0), df.loc[df['real'] == 0, 'real'], color='C7', edgecolors='k')
Finally, we can use ax.fill_between
to plot the shaded areas, as you desired:
ax.fill_between([-5, 35], -0.002, 0, color='C0', zorder=-1, alpha=0.7)
ax.fill_between([-5, 35], 0, 0.002, color='C3', zorder=-1, alpha=0.7)
ax.set_ylim([-0.00015, 0.00015])
ax.set_xlim([-5, 35])
All this together gave me:
Upvotes: 1