import matplotlib.pyplot as plt
# Generate some sample data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a figure and axis object
fig, ax = plt.subplots()
# Plot the data as a line chart
ax.plot(x, y)
# Set the title and axis labels
ax.set_title("Sample Line Chart")
ax.set_xlabel("X Axis Label")
ax.set_ylabel("Y Axis Label")
# Add text labels to each data point
for i in range(len(x)):
ax.text(x[i], y[i], str(y[i]))
# Display the chart
plt.show()
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# Generate some sample data
x = [1, 2, 3, 4, 5]
y = [10, 60, 80, 40, 50]
# Create a figure and axis object
fig, ax = plt.subplots()
# Plot the data as a bar chart
ax.bar(x, y)
# Add text labels to each bar indicating the value
for i, v in enumerate(y):
ax.text(i + 0.8, v + 1, str(v), color='blue', fontweight='bold')
# Set the title and axis labels
ax.set_title("Sample Bar Chart")
ax.set_xlabel("X Axis Label")
ax.set_ylabel("Y Axis Label")
# Create a Tkinter window
root = tk.Tk()
# Embed the Matplotlib figure in the Tkinter window
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()
# Display the window
root.mainloop()
import pandas as pd
pd= pd.read_excel(r'cr.xlsx', skiprows=0, usecols='A:F')
for i in pd.index:
# index默认以0开始,想让序号列以1开始
# 这样发现ID列填充成数值了
pd['序号'].at[i] = i + 1
print(pd)
pd.to_excel('cr.xlsx', index=False) ##数据保存
import pandas as pd
# 读取Excel文件
df = pd.read_excel('12.xlsx')
# 添加序号列
df.insert(0, '序号', range(1, len(df)+1))
# 保存修改后的数据
df.to_excel('new_data.xlsx', index=False)
其中,'插入0
表示将新列插入到第一列,第二个参数'序号'
是新列的名称,第三个参数range(1, len(df)+1)
表示新列的数据,也就是序号。最后,使用to_excel()
方法将修改后的数据保存为新的 Excel 文件。
运行上面的代码后,会得到一个新的 Excel 文件,其中第一列为自动生成的序号列,从 1 开始逐行递增。