Python中matplotlib模块用例(代码)


本文摘自php中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于Python中matplotlib模块用例(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import matplotlib.pyplot as plt

import numpy as np

import requests

url='https://api.github.com/search/repositories?q=language:python&sort=stars'

r=requests.get(url)

print('数据访问状态值:',r.status_code)

print('成功,正常获取网站数据'if r.status_code==200 else '错误,无法获取数据')

response_dict=r.json()  #转换成字典

#print(response_dict)

repo_dicts=response_dict['items']

#print(repo_dicts)

names=[repo_dict['name']for repo_dict in repo_dicts]

print(names)

plot_dicts=[repo_dict['stargazers_count'] for repo_dict in repo_dicts]

print(plot_dicts)

x=np.arange(len(names)) #x轴

plt.bar(x,plot_dicts)   #y轴

plt.plot(x,plot_dicts,'rp--') #折线图

ax=plt.subplot()

ax.set_ylabel('stargazers_count') #y轴标题

ax.set_xlabel('Github Reponstorys')  #x轴标题

ax.set_xticks(x)  #设置每一个x的标题

ax.set_xticklabels(names,rotation=90) #给每一个柱子加上标题

ax.set_title('Github')

#plt.grid(linestyle='--')  #虚线为背景,一个’-‘为实线,俩个为虚线

#plt.show()

#保存图片

import os

imgPath = os.getcwd() + '/images/ch04_demo05_github.jpg'

plt.savefig(imgPath)

print('图片保存成功.')

结果为:

这个其实比较简单,就是将json数据拿出来,并用matplotlib可视化一下就ok了

以上就是本篇文章的全部内容,关于python更多精彩内容大家可以关注php中文网Python视频教程和python文章教程!!!

以上就是Python中matplotlib模块用例(代码)的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python爬虫框架有哪些

Python安装的包如何寻找

Python如何使用matplotlib绘制栈式直方图教程详解

四六级成绩还可以这样查?Python助你装b一步到位!!!

阶乘Python怎么打

Python操作excel详解

如何加速Python程序

你知道Python

7个你必须要知道的Python ide工具

Python怎么加注释

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...