dev_xulongjin 47134d7877 feat(数据可视化技术): 完成实验1任务
- 新增两段文本资源
- 实现词云生成和网络图绘制功能
- 添加《红楼梦》人物关系图交互式展示
2025-04-21 16:27:38 +08:00

231 KiB

None <html lang="en"> <head> </head>
In [1]:
from wordcloud import WordCloud
import matplotlib.pyplot as plt

with open('./resource/1.txt', 'r', encoding="UTF-8") as f:
    text = f.read()

wordcloud = WordCloud().generate(text)

plt.imshow(wordcloud, interpolation="bilinear")

plt.axis("off")

plt.show()
No description has been provided for this image
In [2]:
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import numpy as np

image = np.array(Image.open("resource/3.png"))

with open('./resource/1.txt', 'r', encoding="UTF-8") as f:
    text = f.read()

wordcloud = WordCloud(mask=image, background_color='white').generate(text)

plt.imshow(wordcloud, interpolation="bilinear")

plt.axis("off")

plt.show()
No description has been provided for this image
In [3]:
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba

with open('./resource/2.txt', 'r', encoding="UTF-8") as f:
    text = f.read()

cut_text = " ".join(jieba.cut(text))

cloud = WordCloud(
    background_color="white",
    max_words=100,
    font_path="/System/Library/Fonts/STHeiti Light.ttc",
    min_font_size=40,
    max_font_size=100,
    width=800,
    height=400
)

wCloud = cloud.generate(cut_text)

plt.imshow(wCloud, interpolation="bilinear")
plt.axis("off")
plt.show()
Building prefix dict from the default dictionary ...
Dumping model to file cache /var/folders/d0/kmt7kgdx25z7rddmtqzq253w0000gn/T/jieba.cache
Loading model cost 0.368 seconds.
Prefix dict has been built successfully.
No description has been provided for this image
In [5]:
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
from PIL import Image

image = np.array(Image.open("resource/4.png"))

with open('./resource/2.txt', 'r', encoding="UTF-8") as f:
    text = f.read()

cut_text = " ".join(jieba.cut(text))

cloud = WordCloud(
    mask=image,
    background_color="white",
    max_words=100,
    font_path="/System/Library/Fonts/STHeiti Light.ttc",
    min_font_size=40,
    max_font_size=100,
    width=800,
    height=400
)

wCloud = cloud.generate(cut_text)

plt.imshow(wCloud, interpolation="bilinear")
plt.axis("off")
plt.show()
No description has been provided for this image
In [6]:
from matplotlib import pyplot as plt
import networkx as nx

G = nx.DiGraph()
# 节点
G.add_nodes_from([1, 2, 3, 4, 5, 6, 7])
# 环
nx.add_cycle(G, [1, 2, 3])
# 边
G.add_edge(1, 4)
# 多条边
G.add_edges_from([(3, 5), (3, 6), (6, 7)])
nx.draw_networkx(G)

plt.show()
No description has been provided for this image
In [7]:
from matplotlib import pyplot as plt
import networkx as nx

G = nx.DiGraph()
# 节点
G.add_nodes_from([1, 2, 3, 4, 5, 6, 7])
# 环
nx.add_cycle(G, [1, 2, 3])
# 边
G.add_edge(1, 4)
# 多条边
G.add_edges_from([(3, 5), (3, 6), (6, 7)])
# nx.draw_networkx(G)

node_color = ['yellow', 'blue', 'pink', 'green', 'gray', 'navy', 'indigo']
edge_color = ['orange', 'purple', 'orange', 'cyan']
nx.draw_networkx(G, node_color=node_color, edge_color=edge_color)

plt.show()
No description has been provided for this image
In [8]:
from matplotlib import pyplot as plt
import networkx as nx

G = nx.Graph()
G.add_nodes_from([1])
nx.draw_networkx(G)
plt.show()

G = nx.Graph()
G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2), (1, 3), (1, 4)])
nx.draw_networkx(G)

plt.show()
No description has been provided for this image
No description has been provided for this image
In [9]:
from matplotlib import pyplot as plt
import networkx as nx

G = nx.Graph()
G.add_nodes_from([1])
nx.draw_networkx(G,node_color='purple')
plt.show()

G = nx.Graph()
G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2), (1, 3), (1, 4)])
nx.draw_networkx(G,
                 node_color=['purple','green','blue','red'],
                 edge_color=['pink','orange','navy'])

plt.show()
No description has been provided for this image
No description has been provided for this image
In [ ]:
 
</html>