import numpy as np import csv import pandas as pd import numpy as npjk import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei']#解决图标中汉字显示问题 plt.rcParams['axes.unicode_minus']=False#解决图标中汉字显示问题 from urllib.request import urlopen,Request from bs4 import BeautifulSoup #云计算2113方宇-2021058226 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'} url = 'https://search.jd.com/Search?keyword=%E8%93%9D%E7%89%99%E9%BC%A0%E6%A0%87&enc=utf-8&wq=%E8%93%9D%E7%89%99%E9%BC%A0%E6%A0%87&pvid=405a663911e84dd3822389ef5b97c147' response = Request(url,headers=headers) res = urlopen(response) data = res.read().decode('utf-8') soup = BeautifulSoup(data,'html.parser') ddd=soup.find('ul',class_="gl-warp clearfix") bbb=ddd.select('li>.gl-i-wrap>.p-price>strong>i')#价格 ccc=ddd.select('li>.gl-i-wrap>.p-shop>span>a')#店名 #云计算2113方宇2021058226 get_rmb_date = [] for i in bbb: get_rmb_date.append(i.text) get_shop_date = [] for i in ccc: get_shop_date.append(i.text) data = [] for i in range(len(get_shop_date)): temp = [] temp.append(get_shop_date[i]) temp.append(get_rmb_date[i]) data.append(temp) print(data) #云计算2113-方宇2021058226 f = open('D:/mypython/mycode/2021058226.csv','w',encoding='utf-8') csv_write = csv.writer(f) csv_write.writerow(['商品店家','商品价格']) for i in data: csv_write.writerow(i) f.close() #云计算2113方宇-2021058226 csv_file ='D:/mypython/mycode/2021058226.csv' csv_data=pd.read_csv(csv_file,low_memory=False) csv_df=pd.DataFrame(csv_data) dfl=csv_df.head(n=10) print(dfl) plt.figure(figsize = (10,6)) x = np.arange(10) y = np.array(dfl['商品店家']) xticks = list(dfl['商品价格']) print(x,y,xticks) p=[0,1,2,3,4,5,6,7,8,9] plt.xticks(p,y,rotation=20) plt.bar(p,xticks,color='red') plt.show()如何将词云柱状图按序排列
时间: 2024-04-22 19:23:19 浏览: 201
python数据分析与可视化 import pandas as pd import numpy as np import m
如果想将柱状图按照序排列,可以先对数据进行排序,再绘制柱状图。以下是修改后的代码:
```python
import numpy as np
import csv
import pandas as pd
import numpy as npjk
import matplotlib.pyplot as plt
from wordcloud import WordCloud
plt.rcParams['font.sans-serif']=['SimHei']#解决图标中汉字显示问题
plt.rcParams['axes.unicode_minus']=False#解决图标中汉字显示问题
from urllib.request import urlopen,Request
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}
url = 'https://search.jd.com/Search?keyword=%E8%93%9D%E7%89%99%E9%BC%A0%E6%A0%87&enc=utf-8&wq=%E8%93%9D%E7%89%99%E9%BC%A0%E6%A0%87&pvid=405a663911e84dd3822389ef5b97c147'
response = Request(url,headers=headers)
res = urlopen(response)
data = res.read().decode('utf-8')
soup = BeautifulSoup(data,'html.parser')
ddd=soup.find('ul',class_="gl-warp clearfix")
bbb=ddd.select('li>.gl-i-wrap>.p-price>strong>i')#价格
ccc=ddd.select('li>.gl-i-wrap>.p-shop>span>a')#店名
get_rmb_date = []
for i in bbb:
get_rmb_date.append(float(i.text))
get_shop_date = []
for i in ccc:
get_shop_date.append(i.text)
# 对数据进行排序
data = sorted(zip(get_shop_date, get_rmb_date), key=lambda x: x[1], reverse=True)
get_shop_date, get_rmb_date = zip(*data)
print(data)
f = open('D:/mypython/mycode/2021058226.csv','w',encoding='utf-8')
csv_write = csv.writer(f)
csv_write.writerow(['商品店家','商品价格'])
for i in data:
csv_write.writerow(i)
f.close()
csv_file ='D:/mypython/mycode/2021058226.csv'
csv_data=pd.read_csv(csv_file,low_memory=False)
csv_df=pd.DataFrame(csv_data)
dfl=csv_df.head(n=10)
print(dfl)
plt.figure(figsize = (10,6))
x = np.arange(10)
y = np.array(dfl['商品店家'])
xticks = list(dfl['商品价格'])
print(x,y,xticks)
p = [0,1,2,3,4,5,6,7,8,9]
plt.xticks(p, y, rotation=20)
plt.bar(p, xticks, color='red')
plt.show()
```
这样,柱状图就会按照商品价格的高低进行排列。
阅读全文