0

I'm drawing scatter plot in jupyter notebook which contains 2 cells

from matplotlib import pyplot as plt
import numpy as np

# Plot single point
# 1st, create a figure
fig = plt.figure()
# then create an 'ax' in this figure
ax = fig.add_subplot(111)
# plot red point at x=7, y=42
ax.scatter(x = [7], y = [42])

and

# Plot multiple points
# create a center
center = (7, 42)
# sample scaled normal distribution
datapoints = 10 * np.random.randn(50, 50)
# re-center data
datapoints[0, :] += center[0]
datapoints[1, :] += center[1]
# plot red point for every data-point
ax.scatter(x = datapoints[0, :], y = datapoints[1, :], color = "red")
plt.show()

The first cell returns

enter image description here

whereas the second one returns nothing. Could you please elaborate on this problem?

Update: I tried to put

ax
fig

in the second cell. In particular,

# Plot multiple points
# create a center
center = (7, 42)
# sample scaled normal distribution
datapoints = 10 * np.random.randn(50, 50)
# re-center data
datapoints[0, :] += center[0]
datapoints[1, :] += center[1]
# plot red point for every data-point
ax
fig
ax.scatter(x = datapoints[0, :], y = datapoints[1, :], color = "red")
plt.show()

But running second cell still returns nothing.

Akira
  • 2,594
  • 3
  • 20
  • 45

2 Answers2

0
# Plot multiple points
# create a center
center = (7, 42)
# sample scaled normal distribution
datapoints = 10 * np.random.randn(50, 50)
# re-center data
datapoints[0, :] += center[0]
datapoints[1, :] += center[1]

in this code you added numbers to the X and y numbers which may made your values out of the size of your plt window. try using:

plt.ylim([0,100])
plt.xlim([0,15])

Good luck!

Edit : I also tried your code and it works fine with me when i put both cells into one cell :

enter image description here

  • there's also [python - Automatically Rescale ylim and xlim in Matplotlib - Stack Overflow](https://stackoverflow.com/questions/10984085/automatically-rescale-ylim-and-xlim-in-matplotlib) – user202729 Jan 11 '21 at 13:44
  • Your solution does not work. It only shows a blank plot. – Akira Jan 11 '21 at 13:44
  • 1
    But that's not it. OP said "nothing shows up", not "the old plot show up without any red point". – user202729 Jan 11 '21 at 13:45
  • Did you put all the code in just 1 cell, or 2 separate cells (as I did)? – Akira Jan 11 '21 at 13:52
  • Ahmed, "your code works for me" is not an answer. You two should not describe in more detail the presets of your Jupyter notebooks to find out what the difference is that causes the different behavior. – Mr. T Jan 11 '21 at 13:53
  • i mean it worked fine as the plot shows that i am running both cells together in one cell. – Ahmed Sayed Mansour Jan 11 '21 at 13:56
0

Normally you create the figure at the same cell where you want to show the plot. If those two plots should be independent, just add following line at the beginning of second cell:

# creates figure and axis in one command without any additional settings
fig, ax = plt.subplots()

Or put the ax.scatter() calls into the same cell if you want these scatters to be in the same axis.

Honza S.
  • 172
  • 10