怎么用Python绘制简单的折丝图
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容介绍了"怎么用Python绘制简单的折丝图"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!安装
千家信息网最后更新 2025年12月02日怎么用Python绘制简单的折丝图
本篇内容介绍了"怎么用Python绘制简单的折丝图"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
安装环境matplotlib
个人前面也说了强烈建议使用Pycharm作为Python初学者的首选IDE,主要还是因为其强大的插件功能,很多环境都能一键安装完成,像本文的matplotlib,numpy,requests等。
下面直接上效果图:
绘制简单的折丝图
使用plot来绘制折线
import matplotlib.pyplot as plt# 绘制折线图squares = [1, 4, 9, 16, 25]# plt.plot(squares, linewidth=5) # 指定折线粗细,# #plt.show();## #修改标签文字和线条粗细# plt.title("squre number", fontsize=24)# plt.xlabel("Value", fontsize=14)# plt.ylabel("square of value", fontsize=14)# plt.tick_params(axis='both', labelsize=14)# plt.show()# 校正图形input_values = [1, 2, 3, 4, 5]plt.plot(input_values, squares, linewidth=5)plt.show()
生成的效果图:
使用scatter绘制散点图并设置样式
import matplotlib.pyplot as plt# 简单的点# plt.scatter(2, 4)# plt.show()## # 修改标签文字和线条粗细plt.title("squre number", fontsize=24)plt.xlabel("Value", fontsize=14)plt.ylabel("square of value", fontsize=14)#设置刻度标记大小plt.tick_params(axis='both', which='major', labelsize=14)# 绘制散点x_values = [1, 2, 3, 4, 5]y_values = [1, 4, 9, 16, 25]plt.scatter(x_values, y_values, s=100)plt.show()自动计算数据
import matplotlib.pyplot as pltx_values = list(range(1, 1001))y_values = [x ** 2 for x in x_values]# y_values = [x * x for x in x_values]# y_values = [x ^ 2 for x in x_values]plt.scatter(x_values, y_values, s=40)# 坐标轴的取值范围# plt.axis(0, 1100, 0, 1100000) # 依次是xmin xmax,ymin,ymaxplt.show()
随机漫步
import matplotlib.pyplot as plyfrom random import choiceclass RandomWalk(): def __init__(self, num_points=5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk(self): # 不断走,直到达到指定步数 while len(self.x_values) < self.num_points: # 决定前进方向以及沿这个方向前进的距离 x_direction = choice([1, -1]) x_distance = choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) x_step = x_direction * x_distance y_direction = choice([1, -1]) y_distance = choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y_step = y_direction * y_distance # 不能原地踏步 if x_step == 0 and y_step == 0: continue next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.append(next_x) self.y_values.append(next_y)rw = RandomWalk()rw.fill_walk()ply.scatter(rw.x_values, rw.y_values, s=15)ply.show()
效果图
使用Pygal模拟掷骰子
pygal能够绘制的图形可以访问pygal介绍
环境安装,直接在Pycharm上安装插件。
import pygalfrom random import randintclass Die(): def __init__(self, num_sides=6): self.num_sides = num_sides; def roll(self): # 返回一个位于1和骰子面数之间的随机值 return randint(1, self.num_sides)die = Die()results = []# 掷100次骰子,并将结果放在列表中。for roll_num in range(10): result = die.roll() results.append(str(result))print(results)# 分析结果frequencies = []for value in range(1, die.num_sides + 1): frequency = results.count(value) frequencies.append(frequency)print(frequencies)# 对结果进行可视化hist = pygal.Box()hist.title = "result of rolling one D6 1000 times"hist.x_labels = ['1', '2', '3', '4', '5', '6']hist.x_title = "Result"hist.y_title = "frequency of result"hist.add('D6', frequencies)hist.render_to_file('die_visual.svg')使用Web API
1.1安装requests
这个可以直接在Pycharm中安装插件,非常方便。
1.2处理API响应
import requests# 执行api调用并存储响应url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'r = requests.get(url)print("Status code:", r.status_code)# 将api响应存储在一个变量中response_dic = r.json()# 处理结果print(response_dic.keys())得到结果:Status code: 200dict_keys(['total_count', 'incomplete_results', 'items'])1.3处理响应字典
# 将api响应存储在一个变量中response_dic = r.json()# 处理结果print(response_dic.keys())print("Total repositories:", response_dic['total_count'])repo_dics = response_dic['items']print("repositories returned:" + str(len(repo_dics)))# 研究一个仓库repo_dic = repo_dics[0]print("\nKeys:", str(len(repo_dic)))# for key in sorted(repo_dic.keys()):# print(key)print("Name:", repo_dic['name'])print("Owner:", repo_dic['owner']['login'])print("Starts:", repo_dic['stargazers_count'])print("Repository:", repo_dic['html_url'])print("Created:", repo_dic['created_at'])print("Updated:", repo_dic['updated_at'])print("Description:", repo_dic['description'])得到结果:Total repositories: 2061622repositories returned:30Keys: 71Name: awesome-pythonOwner: vintaStarts: 40294Repository: https://github.com/vinta/awesome-pythonCreated: 2014-06-27T21:00:06ZUpdated: 2017-10-29T00:50:49ZDescription: A curated list of awesome Python frameworks, libraries, software and resources"怎么用Python绘制简单的折丝图"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
结果
处理
插件
效果
效果图
环境
粗细
骰子
存储
内容
变量
图形
折线
文字
方向
更多
标签
知识
线条
强大
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
乐乐大作战软件开发商
财经互联网科技
津南区网络安全教育
qt数据库标签
上海品质软件开发服务创意
网络安全工程专业有什么课程
集美大学思科网络技术基础
网络安全技术调查报告
obsrtsp 服务器
福建趋链软件开发有限公司
c 数据库添加代码怎么写
数据库中的记录是实体吗
幻塔服务器未准备好怎么回事
我的世界水桶服务器怎么进
照片管理服务器不可用
虚拟机服务器 资料安全
pg数据库loop循环
pl sql数据库表备份
网络技术负责人需要做什么
安次区租房软件开发
kegg子数据库
网络安全能从事什么工作
一个系统pc端和移动端数据库
绝地求生2连接不了服务器
天津什么软件开发服务值得推荐
网络安全系统漏洞问题
爱管家网络安全教育平台
怎样对罗斯文数据库压缩备份
广州市力智慧互联网科技
华中科大网络安全专业就业前景