数据库
首页 > 数据库> > python – 从数据库中提取的10k记录的散点图

python – 从数据库中提取的10k记录的散点图

作者:互联网

我试图在Python中制作散点图.我认为它会相当简单,但在绘图时会遇到散点图(x和y值)的理解.

==我的使命==

>我有数据库和超过10k记录(所有浮动)到现在,并将每天增加.
>记录范围为200-2000(以十进制浮点数表示).
>所以,我希望在我的数据集中看到人口最多的区域.

==我做了什么?==

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()

散点图了解(link)

好!这个情节没有任何意义.

==问题==

>如何制作散点图以查看人口最多的地区?
>如何指定y变量以使x变量具有相同的维度(获取的记录总数)?

新的绘图和统计,所以请帮助我

解决方法:

也许你正在寻找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()

您还可以使用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()

关于pylab的使用:pyplot的docstring说

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.

标签:scatter-plot,python,mysql,matplotlib
来源: https://codeday.me/bug/20190902/1792123.html