import pandas as pd from matplotlib import pyplot as plt from matplotlib import rcParams from config import * rcParams['font.family'] = 'simhei' df = pd.read_excel(file_name, dtype={ '年份': int, '值': float }) ax = df.plot(x='年份', y='值', figsize=(15, 10), kind=pandas_type) if pandas_type == 'line' or pandas_type == 'scatter': ax.set_xticks(df['年份']) unit = df['单位'].to_list()[0] ax.tick_params(axis='both', labelsize=14) ax.set_xlabel("年份", fontsize=14) ax.set_ylabel(f'值(单位:{unit})', fontsize=14) ax.set_title(title, fontsize=16) plt.show()
时间: 2023-11-28 20:03:57 浏览: 75
这段代码的功能是读取一个Excel文件并绘制数据的可视化图形。其中使用了 Pandas 库来读取 Excel 文件,Matplotlib 库来绘制图形。代码中的 rcParams 部分用于设置字体,保证中文显示正常。函数的参数中包括文件名、绘图类型、标题等。绘制出的图形可以是线图或散点图,根据不同的类型设置不同的参数。最后调用 plt.show() 显示绘制出的图形。
相关问题
import numpy as np import pandas as pd import matplotlib import matplotlib.pyplot as plt import seaborn as sns import chardet
### 正确导入Python数据分析和可视化库的方法
为了进行高效的数据分析与可视化,在Python环境中正确安装并导入必要的库至关重要。以下是关于`numpy`, `pandas`, `matplotlib`, `seaborn` 和 `chardet` 的具体导入方法:
#### 导入库
在开始任何项目之前,确保已经安装了所需的软件包。如果尚未安装这些库,可以使用pip命令来完成安装。
```bash
pip install numpy pandas matplotlib seaborn chardet
```
一旦确认所需库已成功安装,则可以在脚本顶部按照如下方式依次引入各个模块:
```python
import numpy as np # 提供多维数组对象以及派生对象(如掩码数组、矩阵),并且支持大量的函数操作。
import pandas as pd # 数据处理和分析的核心工具之一;提供了DataFrame结构用于存储表格型数据集。
import matplotlib.pyplot as plt # Python中最流行的绘图库之一,能够创建静态、动态交互式的图表。
import seaborn as sns # 基于Matplotlib之上构建而成的统计图形库,简化了许多常见的统计数据可视化的实现过程。
import chardet # 自动检测字符编码类型的实用程序,对于读取未知编码格式文件非常有用。
```
设置字体以便正常显示中文标签和其他特殊符号也是重要的一步。可以通过修改`matplotlib`的相关参数来进行配置:
```python
plt.rcParams['font.sans-serif'] = 'SimHei' # 设置默认字体为黑体以支持中文显示
plt.rcParams['axes.unicode_minus'] = False # 解决负号无法正确显示的问题
```
上述代码片段展示了如何准备环境,使得后续的数据处理工作更加顺畅[^1]。
分析下列代码:import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel("E:\\爬取\\爬取.xls") year_count = data.groupby(by="年份")["年份"].count() data['年份'] = pd.to_numeric(data['年份'], errors='coerce') plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False plt.plot(year_count.index, year_count.values) plt.title("电影年份发展折线图") plt.xlabel("年份") plt.ylabel("数量") plt.show() import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel("E:\\爬取\\爬取.xls") plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False year_count = data['国家'].value_counts() plt.bar(year_count.index, year_count.values) plt.title("各个国家电影数量") plt.xlabel("国家") plt.ylabel("电影数量") plt.show() import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel("E:\\爬取\\爬取.xls") plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False type_count = data["类型"].value_counts() plt.pie(type_count,labels=type_count.index,pctdistance=0.8,autopct='%1.1f%%',explode=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.01]) plt.figure(figsize=(20,6.5)) plt.title("电影主要类型分布图") plt.show() import pandas as pd import matplotlib.pyplot as plt data = pd.read_excel("E:\\爬取\\爬取.xls") scores = data["排名"].values votes = data["评价人数"].values plt.rcParams["font.sans-serif"] = ["SimHei"] plt.scatter(scores, votes) plt.title("排名与评价人数散点图") plt.xlabel("排名") plt.ylabel("评价人数") plt.show()
这段代码主要是用来对爬取到的电影数据进行数据可视化的处理。在代码中使用了 pandas 库对 Excel 数据进行读取和处理,使用了 matplotlib 库来绘制各种图表。
第一个代码块绘制了电影年份的发展折线图,其中先对年份进行了分组统计,然后使用 plot 函数将结果绘制出来。
第二个代码块绘制了各个国家电影数量的柱状图,其中使用了 value_counts 函数对国家进行统计,然后使用 bar 函数将结果绘制出来。
第三个代码块绘制了电影主要类型分布的饼图,其中使用了 value_counts 函数对电影类型进行统计,然后使用 pie 函数将结果绘制出来。
第四个代码块绘制了排名与评价人数的散点图,其中使用了 scatter 函数将排名和评价人数进行散点绘制。
阅读全文
相关推荐

















