f=open('D:/mycode/2021058242.txt','r',encoding ='utf-8').read() #f_text=jieba.lcut(f) font = r'C:\Windows\Fonts\SIMLI.ttf' stopwords=',\\n“”'#构造停用字符 cut_list = jieba.lcut(''.join(re.findall('[\u4e00-\u9fa5]', f)), cut_all = False) #U4300-U9fa5代表了符合汉字GB18030规范的字符集 # for i in range(len(cut_list)-1, -1, -1): # if cut_list[i] in stopwords: # del cut_list[i] #print(cut_list) f_text=' '.join(cut_list)#把清洗过的分词结果拼接起来,必须用空格分隔开 #print(f_text) wc1 = WordCloud(font_path=font).generate(f_text)#生成词云的对象是已经分词过的词语 # 词云展示 plt.imshow(wc1, interpolation='bilinear') plt.axis('off') plt.show()怎么美化词云
时间: 2024-04-22 07:23:51 浏览: 153
美化词云可以通过调整参数来实现,例如修改背景颜色、调整字体大小和颜色、添加形状等。下面是一些常用的美化词云的方法:
1. 设置背景颜色:可以通过 background_color 参数来设置背景颜色,例如:`wc1 = WordCloud(background_color="white", font_path=font).generate(f_text)`
2. 调整字体大小和颜色:可以通过 font_size 和 color_func 参数来调整字体大小和颜色,例如:`wc1 = WordCloud(font_path=font, font_size=50, color_func=lambda *args, **kwargs: (255,0,0)).generate(f_text)`
3. 添加形状:可以通过 mask 参数来添加形状,例如:`mask = np.array(Image.open("mask.png")) wc1 = WordCloud(font_path=font, mask=mask).generate(f_text)`
4. 调整词云布局:可以通过调整词云布局来实现美化,例如:`wc1 = WordCloud(font_path=font, layout=3).generate(f_text)`
你可以根据需要选择合适的方法来美化词云。
相关问题
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()如何将词云柱状图按序排列
如果想将柱状图按照序排列,可以先对数据进行排序,再绘制柱状图。以下是修改后的代码:
```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()
```
这样,柱状图就会按照商品价格的高低进行排列。
import radon from radon.visitors import ComplexityVisitor class MyClass: def my_function(self): for i in range(10): if i % 2 == 0: print(i) else: print("odd") file_path = "accuracy_rate.ipynb" with open(file_path, "r",encoding='utf-8') as f: code = f.read() visitor = ComplexityVisitor.from_code(code) print("圈复杂度:", str(visitor.total_complexity())) raw_loc = radon.raw_analysis.analyze(code) print("代码行数:", str(raw_loc.loc)) function_list = radon.raw_analysis.extract_functions(code) print("函数数量:", str(len(function_list))) duplication = radon.complexity.cc_duplicate(code) print("代码重复率:", str(duplication))怎么修改
这段代码存在一些问题,可能是因为复制粘贴时出现了错误。以下是修改后的代码:
```python
import radon
from radon.visitors import ComplexityVisitor
class MyClass:
def my_function(self):
for i in range(10):
if i % 2 == 0:
print(i)
else:
print("odd")
file_path = "example.py"
with open(file_path, "r", encoding='utf-8') as f:
code = f.read()
visitor = ComplexityVisitor.from_code(code)
print("圈复杂度:", str(visitor.total_complexity()))
raw_loc = radon.raw_analysis.analyze(code)
print("代码行数:", str(raw_loc))
function_list = radon.raw_analysis.extract_functions(code)
print("函数数量:", str(len(function_list)))
duplication = radon.complexity.cc_duplicate(code)
print("代码重复率:", str(duplication))
```
修改点:
1. `import radon` 应该改为 `import radon`。
2. `print("代码行数:", str(raw_loc.loc))` 应该改为 `print("代码行数:", str(raw_loc))`。
3. 文件路径 `file_path` 应该指向一个存在的 Python 文件。
4. 代码中的缩进应该是使用四个空格,而不是使用两个空格。
阅读全文