幫我寫一個基於pandas numpy 庫和python語言進行數據處理 首先要讀取在D大學大二下學期數據可視化期末作業數據整理被引次數xlsx裏的excel檔並對檔案中的被引频次進行降序並抽出前十的數據對X軸設置間距 可視化圖上要顯示中文字符 不讓中文字符重疊最後並用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/hxwT 著作权归作者所有。请勿转载和采集!