How to visualize text data using Python?

Python can use a variety of libraries to implement text data visualization, the most commonly used of which include matplotlib, seaborn, pandas, WordCloud, etc. The following are some basic text data visualization practices:

1. Draw a column chart

Draw a histogram: Use the matplotlib library to draw a histogram. You can use the x-axis to represent data and the y-axis to represent values. You can use the bar function to draw a bar chart.

import matplotlib.pyplot as plt  
  
# 示例数据  
x = ['apple', 'banana', 'orange', 'grape']  
y = [15, 25, 10, 20]  
  
# 绘制柱状图  
plt.bar(x, y)  
  
# 设置标题和坐标轴标签  
plt.title('Fruit Sales')  
plt.xlabel('Fruit')  
plt.ylabel('Sales')  
  
# 显示图形  
plt.show()

Insert image description here

2. Draw a scatter plot

Draw a scatter plot: Use the seaborn library to draw a scatter plot. You can use the x-axis to represent data and the y-axis to represent values. Scatter plots can be drawn using the scatter function.

import seaborn as sns  
import matplotlib.pyplot as plt  
  
# 示例数据  
x = ['apple', 'banana', 'orange', 'grape']  
y = [15, 25, 10, 20]  
  
# 绘制散点图  
sns.scatterplot(x=x, y=y)  
  
# 设置标题和坐标轴标签  
plt.title('Fruit Sales')  
plt.xlabel('Fruit')  
plt.ylabel('Sales')  
  
# 显示图形  
plt.show()

Insert image description here

3. Draw a heat map

Draw a heat map: Use the pandas library to draw a heat map. You can use the x-axis to represent data and the y-axis to represent values. Heat maps can be implemented using the pivot_table function.

import numpy as np 
import matplotlib.pyplot as plt  
  
# 示例数据  
data = np.random.rand(10, 12)
ax = sns.heatmap(data)
  
# 绘制热力图  
plt.imshow(data,cmap='hot')  
  
# 设置标题和坐标轴标签  
plt.title('Fruit Sales')  
plt.xlabel('Fruit')  
plt.ylabel('Sales')  
  
# 显示图形  
plt.show()

Insert image description here

4. Draw a word cloud diagram

To draw a word cloud chart, you can use the wordcloud library in Python. First, use pip install wordcloud to install the library. After importing the text data, create a WordCloud object, set the background color, width, and height of the word cloud chart, and use the generate() method to convert the text Pass it to the word cloud object to generate the word cloud graph. Finally, use the imshow() method to display the word cloud graph, and use the axis() method to hide the coordinate axis.

import matplotlib.pyplot as plt  
from wordcloud import WordCloud  
  
text = "This is some sample text for generating a word cloud."  
  
# 创建词云对象  
wordcloud = WordCloud(background_color='white', width=800, height=600).generate(text)  
  
# 显示词云图  
plt.figure(figsize=(9, 6))  
plt.imshow(wordcloud, interpolation='bilinear')  
plt.axis("off")  
plt.show()

Insert image description here

The above is the basic text data visualization practice. You can choose the appropriate library and method to implement according to specific needs, so as to realize text data analysis. I hope it can inspire you through learning. We can see that Python is used in data processing and data analysis. It has unique advantages in data visualization, making data analysis easy~

If you also want to get started with data analysis, here are some free courses for everyone to learn. Below are screenshots of the courses. Scan the QR code at the bottom to get them all.

1. Python learning routes in all directions

Insert image description here

2. Learning software

If a worker wants to do his job well, he must first sharpen his tools. The commonly used development software for learning Python is here, saving everyone a lot of time.
Insert image description here

3. Study materials

Insert image description here

4. Practical information

Practice is the only criterion for testing truth. The compressed packages here can help you improve your personal abilities in your spare time.
Insert image description here

5. Video courses

Insert image description here

Well, today’s sharing ends here. Happy time is always short. Friends who want to learn more courses, don’t worry, there are more surprises~Insert image description here

Guess you like

Origin blog.csdn.net/Everly_/article/details/133376901