matplotlib+numpy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sersha
    New Member
    • Sep 2018
    • 1

    matplotlib+numpy

    My problem statement is the following:
    Use NumPy and Matplotlib to draw a scatterplot of uniform random (x, y) values all drawn from the [0, 1] interval.

    The following is what I have tried:
    Code:
    import matplotlib.pyplot as plt
    
    import numpy as np
    
    import random
    
    # Fixing random state for reproducibility
    
    np.random.seed(19680801)
     
    N = 50
    
    x = random.uniform(0, 1)
    y = random.uniform(0, 1)
    
    colors = random.uniform(0, 1)
    
    #for ...
    
    plt.scatter(x, y, s=area, c=colors, alpha=0.5)
    plt.title('Exercise 2 - Drawing a plot')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()
    The output of the above is my plot with a single large dot in the center.
    For some reason I am only generating and plotting a single x,y pair.
    I think I need a for loop but I lack knowledge of matplotlib+nump y.

    Is anyone able to help me with this?
  • s0up1
    New Member
    • Jun 2019
    • 1

    #2
    Use list comprehension to generate a list of values!

    ```
    x = [random.uniform( 0, 1) for x in range(100)]
    y = [random.uniform( 0, 1) for x in range(100)]
    ```

    Comment

    Working...