Home > Mobile >  Python Scatter plot with matrix input. Having trouble getting number of columns showing on x axis, t
Python Scatter plot with matrix input. Having trouble getting number of columns showing on x axis, t

Time:12-02

I'm making a bar chart and a scatter plot. The bar chart takes a vector as an input. I plotted the values on the x-axis, and the amount of times they repeat on the y-axis. This is did by converting the vector to a list and using .count(). That worked great and was relatively straightforward.

As for the scatterplot, the input is going to be a matrix of any x and y dimensions. The idea is to have the amount of columns in the matrix show up on the x axis going from 1,2,3,4 etc depending on how many columns the inserted matrix is. The rows of each column will consist of many different numbers that I would like all to be displayed as dots or stars above the relevant column index, i. e. Column #3 consists of values 6,2,8,5,9,5 going down, and would like a dot for each of them going up the y-axis directly on top of the number 3 on the x axis. I have tried different approaches, some with dots showing up but in wrong places, other times the x axis is completely off even though I used .len(0,:) which prints out the correct amount of columns but doesn't chart it.

My latest attempt which now doesn't even show the dots or stars:

import numpy as np # Import NumPy
import matplotlib.pyplot as plt # Import the matplotlib.pyplot module

vector = np.array([[-3,7,12,4,0o2,7,-3],[7,7,12,4,0o2,4,12],[12,-3,4,10,12,4,-3],[10,12,4,0o3,7,10,12]])

x = len(vector[0,:])
print(x)#vector[0,:]


y = vector[:,0]
plt.plot(x, y, "r.") # Scatter plot with blue stars
plt.title("Scatter plot") # Set the title of the graph
plt.xlabel("Column #") # Set the x-axis label
plt.ylabel("Occurences of values for each column") # Set the y-axis label
plt.xlim([1,len(vector[0,:])]) # Set the limits of the x-axis
plt.ylim([-5,15]) # Set the limits of the y-axis
plt.show(vector) 

The matrix shown at the top is just one I made up for the purpose of testing, the idea is that it should work for any given matrix which is imported.

I tried the above pasted code which is the closest I have gotten as it actually prints the amount of columns it has, but it doesn't show them on the plot. I haven't gotten to a point where it actually plots the points above the columns on y axis yet, only in completely wrong positions in a previous version.

CodePudding user response:

import numpy as np # Import NumPy
import matplotlib.pyplot as plt # Import the matplotlib.pyplot module

vector = np.array([[-3,7,12,4,0o2,7,-3],
                   [7,7,12,4,0o2,4,12],
                   [12,-3,4,10,12,4,-3],
                   [10,12,4,0o3,7,10,12]])

rows, columns = vector.shape
plt.title("Scatter plot") # Set the title of the graph
plt.xlabel("Column #") # Set the x-axis label
plt.ylabel("Occurences of values for each column") # Set the y-axis label
plt.xlim([1,columns]) # Set the limits of the x-axis
plt.ylim([-5,15]) # Set the limits of the y-axis

for i in range(1, columns 1):
    y = vector[:,i-1]
    x = [i] * rows
    plt.plot(x, y, "r.")

plt.show()
  • Related