4

I have one short question: is there any way to show the points in a scatterplot as plus and minus signs? For example, this code produces a scatter plot with blue and red points:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame({'x': np.arange(25), 'y': np.random.normal(0,2500,25)})
       fig, ax = plt.subplots()
ax.scatter(df.x, df.y, c=np.sign(df.y), cmap="bwr")
plt.show()

I would like have blue points as blue plus signs and red points as red minus signs. Is it possible? Thanks in advance.

jpnadas
  • 774
  • 6
  • 17
Logic_Problem_42
  • 229
  • 2
  • 11

2 Answers2

5

You can add this argument to ax.scatter marker="+" For a full list of markers see the official documentation

If you want conditional markers you can assign markers as an array where you run an if clause, similar to this previous SO thread

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Axblert
  • 556
  • 4
  • 19
  • How do you actually propose plotting it? Are you recommending a loop with a separate call to `scatter` for each point? Because your plot will become unmanageable very quickly in that case. – Mad Physicist Dec 22 '19 at 10:13
  • It must have solved part of his problem but I agree masking is a better way IF his main concern was using multiple markers at once. – Axblert Dec 22 '19 at 10:20
  • @Axblert. I've added an answer to the question you linked to. It would be nice if you posted the code you're suggesting instead of just linking to it. – Mad Physicist Dec 22 '19 at 10:29
5

scatter only accepts a single marker style per call, so you would have to plot each marker separately. This is fairly easy using a mask:

mask = df.y >= 0
ax.scatter(df.x[mask], df.y[mask], c='b', marker='+')
ax.scatter(df.x[~mask], df.y[~mask], c='r', marker='_')
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264