python 调取MySQL数据库数据绘制散点图

 效果图

代码

import pymysql
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
#建立一个MySQL连接
connect = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123456',
    database='test',
    charset='utf8'
    )
# 获得游标
cursor = connect.cursor()
# 创建插入SQL语句
sql ="""
select * from crack_growth_rate
"""
cursor.execute(sql)
data = cursor.fetchall()
print(data)

#定义坐标轴字体
plt.rc('font',family='Times New Roman')
#设置坐标轴刻度字号
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
#设置坐标轴显示范围
plt.xlim(1,100)
plt.ylim(1E-5,1E-3)
#设置坐标轴线粗细
ax=plt.gca()#获得坐标轴的句柄
ax.spines['bottom'].set_linewidth(1)#设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(1)#设置左边坐标轴的粗细
# 设置坐标轴标签字体大小
plt.tick_params(labelsize=10)
plt.xlabel("Cyclic Stress Intensity Factor ΔK(MPa*m^0.5)",fontsize=12,labelpad=1)
plt.ylabel("Crack growth rate da/dN(mm/cycle)",fontsize=12,labelpad=1)
#设置坐标轴以固定间隔显示刻度
x_major_locator=MultipleLocator(10)#以每10显示
y_major_locator=MultipleLocator(10)#以每10显示
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.xscale('log')
plt.yscale('log')

ls = []
x_values1 = []
y_values1 = []
x_values2 = []
y_values2 = []
x_values3 = []
y_values3 = []
for i in range(0,len(data)):
    ls.append(list(data[i]))
print(ls)

for j in range(0,22):
    x_values1.append(float(eval(ls[j][1])))
    y_values1.append(eval(ls[j][2]))
    legend_name1 = ls[0][0]

for k in range(22,62):
    x_values2.append(float(eval(ls[k][1])))
    y_values2.append(eval(ls[k][2]))
    legend_name2 = ls[22][0]

for m in range(62,128):
    x_values3.append(float(eval(ls[m][1])))
    y_values3.append(eval(ls[m][2]))
    legend_name3 = ls[62][0]

line1, = plt.loglog(x_values1,y_values1,'.', color='green',marker='^',markersize=3,label=legend_name1)
line2, = plt.loglog(x_values2,y_values2,'.', color='blue',marker='*',markersize=3,label=legend_name2)
line3, = plt.loglog(x_values3,y_values3,'.', color='red',marker='o',markersize=3,label=legend_name3)
plt.legend(handles=[line1, line2,line3], loc='best')
plt.show()






优化代码

import pymysql
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
#建立一个MySQL连接
connect = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123456',
    database='test',
    charset='utf8'
    )
# 获得游标
cursor = connect.cursor()
# 创建插入SQL语句
sql ="""
select * from crack_growth_rate
"""
cursor.execute(sql)
data = cursor.fetchall()
print(data)

#定义坐标轴字体
plt.rc('font',family='Times New Roman')
#设置坐标轴刻度字号
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
#设置坐标轴显示范围
plt.xlim(1,100)
plt.ylim(1E-5,1E-3)
#设置坐标轴线粗细
ax=plt.gca()#获得坐标轴的句柄
ax.spines['bottom'].set_linewidth(1)#设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(1)#设置左边坐标轴的粗细
# 设置坐标轴标签字体大小
plt.tick_params(labelsize=10)
plt.xlabel("Cyclic Stress Intensity Factor ΔK(MPa*m^0.5)",fontsize=12,labelpad=1)
plt.ylabel("Crack growth rate da/dN(mm/cycle)",fontsize=12,labelpad=1)
#设置坐标轴以固定间隔显示刻度
x_major_locator=MultipleLocator(10)#以每10显示
y_major_locator=MultipleLocator(10)#以每10显示
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
plt.xscale('log')
plt.yscale('log')
#数据显示
ls = []
for i in range(0,len(data)):
    ls.append(list(data[i]))
print(ls)
def shuju(start,end,dtype=int):
    x_values = []
    y_values = []
    for j in range(start, end):
        x_values.append(float(eval(ls[j][1])))
        y_values.append(eval(ls[j][2]))
        legend_name = ls[int(start)][0]
        line1, = plt.loglog(list(x_values), list(y_values), '.', color='green', marker='^', markersize=3,
                            label=legend_name)
    return x_values,y_values,legend_name,line1,
if __name__ =='__main__':
    (x_values, y_values, legend_name,line1,) = shuju(0,22)
    line1, = plt.loglog(list(x_values),list(y_values), '.', color='green', marker='^', markersize=3, label=legend_name)
    (x_values, y_values, legend_name) = shuju(22, 62)
    line2, = plt.loglog(list(x_values), list(y_values), '.', color='blue', marker='*', markersize=3,label=legend_name)
    (x_values, y_values, legend_name) = shuju(62, 128)
    line3, = plt.loglog(list(x_values), list(y_values), '.', color='red', marker='o', markersize=3,label=legend_name)
    plt.legend(handles=[line1, line2,line3],loc='best')
    plt.show()






数据

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值