发布于2019-08-07 12:54 阅读(1009) 评论(0) 点赞(1) 收藏(4)
Python数据分析
Python爬取网页数据
// An highlighted block
import requests
if __name__=="__main__":
response = requests.get("https://book.douban.com/subject/26986954/")
content = response.content.decode("utf-8")
print(content)
// An highlighted block
import requests
url="https://pro.jd.com/mall/active/4BNKTNkRMHJ48QQ5LrUf6AsydtZ6/index.html"
try:
r=requests.get(url)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[:100])
except:
print("爬取失败")
Python生成柱状图
// An highlighted block
import matplotlib.pyplot as plt
num_list = [1.5,0.6,7.8,6]
plt.bar(range(len(num_list)), num_list,color='rbgy')
plt.show()
Python生成堆状柱状图
// An highlighted block
import matplotlib.pyplot as plt
name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,0.6,7.8,6]
num_list1 = [1,2,3,1]
plt.bar(range(len(num_list)), num_list, label='boy',fc = 'y')
plt.bar(range(len(num_list)), num_list1, bottom=num_list, label='girl',tick_label = name_list,fc = 'r')
plt.legend()
plt.show()
Python生成竖状柱状图
// An highlighted block
import matplotlib.pyplot as plt
name_list = ['Monday','Tuesday','Friday','Sunday']
num_list = [1.5,0.6,7.8,6]
num_list1 = [1,2,3,1]
x =list(range(len(num_list)))
total_width, n = 0.8, 2
width = total_width / n
plt.bar(x, num_list, width=width, label='boy',fc = 'y')
for i in range(len(x)):
x[i] = x[i] + width
plt.bar(x, num_list1, width=width, label='girl',tick_label = name_list,fc = 'r')
plt.legend()
plt.show()
Python生成折线图
// An highlighted block
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(15, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
Python生成柱状图
// An highlighted block
import pandas as pd
import numpy as np
df = pd.DataFrame(3 * np.random.rand(5), index=['a', 'b', 'c', 'd','e'], columns=['x'])
df.plot.pie(subplots=True)
Python生成箱型图
// An highlighted block
#首先导入基本的绘图包
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#添加成绩表
plt.style.use("ggplot")
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['font.sans-serif']=['SimHei']
#新建一个空的DataFrame
df=pd.DataFrame()
// An highlighted block
df["英语"]=[76,90,97,71,70,93,86,83,78,85,81]
df["经济数学"]=[65,95,51,74,78,63,91,82,75,71,55]
df["西方经济学"]=[93,81,76,88,66,79,83,92,78,86,78]
df["计算机应用基础"]=[85,78,81,95,70,67,82,72,80,81,77]
df
// An highlighted block
plt.boxplot(x=df.values,labels=df.columns,whis=1.5)
plt.show()
// An highlighted block
#用pandas自带的画图工具更快
df.boxplot()
plt.show()
Python生成正态分布图
// An highlighted block
# -*- coding:utf-8 -*-
# Python实现正态分布
# 绘制正态分布概率密度函数
import numpy as np
import matplotlib.pyplot as plt
import math
u = 0 # 均值μ
u01 = -2
sig = math.sqrt(0.2) # 标准差δ
x = np.linspace(u - 3 * sig, u + 3 * sig, 50)
y_sig = np.exp(-(x - u) ** 2 / (2 * sig ** 2)) / (math.sqrt(2 * math.pi) * sig)
print(x)
print("=" * 20)
print(y_sig)
plt.plot(x, y_sig, "r-", linewidth=2)
plt.grid(True)
plt.show()
喜欢的小伙伴可以尝试一下哦
作者:遥远的她
链接:https://www.pythonheidong.com/blog/article/11059/9afe6c460eee16496b6e/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!