Python的一些应用

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)
在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页