Reputation: 368
I am trying to make a scatter plot in Python. I supposed it will be fairly simple but got stuck with understanding in scatterplot (x and y value) while plotting.
==My mission ==
==What I did?==
import numpy as np
import pylab as pl
import MySQLdb
import sys
import math
conn = MySQLdb.connect(
host="localhost",
user="root",
passwd="root",
db="myproject")
with conn:
cur = conn.cursor()
#will fetch all recoreds called monoiso field
cur.execute("SELECT monoiso FROM pmass_selectedion")
rows = cur.fetchall()
for row in rows:
#xvalue for monoiso variable and yvalue for range
xvalue = row
yvalue = [600]
# tried this way too but got x and y dimension error
#yvalue = [400,800,1200,1600]
pl.plot(xvalue,yvalue,'ro')
pl.show()
Scatterplot Understanding (link)
Ok! this plot doesnt make any sense.
==Question ==
New to plotting and statistic so please help me out
Upvotes: 3
Views: 941
Reputation: 879701
Perhaps you are looking for a matplotlib histogram:
import numpy as np
import MySQLdb
import matplotlib.pyplot as plt # This is meant for scripts
# import pylab as pl # This is meant for interactive sessions;
import operator
conn = MySQLdb.connect(
host="localhost",
user="root",
passwd="root",
db="myproject")
with conn:
cur = conn.cursor()
#will fetch all recoreds called monoiso field
cur.execute("SELECT monoiso FROM pmass_selectedion")
rows = cur.fetchall()
monoisos = [row[0] for row in rows]
# Make a histogram of `monoisos` with 50 bins.
n, bins, histpatches = plt.hist(monoisos, 50, facecolor = 'green')
plt.show()
You can also make a histogram/dot-plot by using numpy.histogram:
momoisos = [row[0] for row in rows]
hist, bin_edges = np.histogram(monoisos, bins = 50)
mid = (bin_edges[1:] + bin_edges[:-1])/2
plt.plot(mid, hist, 'o')
plt.show()
Regarding the use of pylab: The docstring for pyplot says
matplotlib.pylab
combines pyplot with numpy into a single namespace. This is convenient for interactive work, but for programming it is recommended that the namespaces be kept separate.
Upvotes: 3
Reputation: 820
For a scatter plot, you need an equal number of x and y values. Usually in a scatter plot, one of the variables is a function of the other one, or at least both have numerical values. For example you could have x values [1, 2, 3] and y values [4, 5, 6], so then on a 2-dimensional plot, the (x, y) values of (1, 4), (2, 5) and (3, 6) will be plotted.
In your case, it seems to me there are no y-values, but only x values, and you are keeping y fixed. From what it seems to me we cannot generate a scatter plot like this. We need one y value corresponding to each x value. You could try serial numbers as y, but it might not make much sense in the plot.
Upvotes: 2