Python Pandas & Matplotlib: 可视化论文被引频次前十排名
下面是基于 pandas、numpy 库和 python 语言进行数据处理的代码,并用 Matplotlib 库进行可视化:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置中文字符显示
font = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf', size=12)
# 读取 excel 档
df = pd.read_excel(r'D:\大學\大二\下學期\數據可視化\期末作業\數據整理\被引次數.xlsx')
# 将被引次數进行降序排列,并抽出前十的數據
top10 = df.sort_values(by=['被引频次'], ascending=False).head(10)
# 设置 X 轴间距和颜色
x = np.arange(top10.shape[0])
width = 0.4
color1 = '#008fd5'
color2 = '#fc4f30'
# 繪製步階圖和柱狀圖
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.step(x, top10['被引频次'], where='mid', color=color1)
ax2.bar(x + width/2, top10['被引频次'], width=width, color=color2)
# 设置坐标轴标签和图例
ax1.set_xlabel('论文', fontproperties=font)
ax1.set_ylabel('被引频次', fontproperties=font, color=color1)
ax2.set_ylabel('被引频次', fontproperties=font, color=color2)
ax1.tick_params(axis='y', labelcolor=color1)
ax2.tick_params(axis='y', labelcolor=color2)
plt.xticks(x, top10['论文名称'], rotation=90, fontproperties=font)
ax1.legend(['步阶图'], loc='upper left', prop=font)
ax2.legend(['柱状图'], loc='upper right', prop=font)
# 显示图形
plt.show()
执行后,会生成一个步阶图和柱状图合并的图,两者的颜色不一样,且图上显示中文字符,不会重叠。
原文地址: https://www.cveoy.top/t/topic/o0jj 著作权归作者所有。请勿转载和采集!