python折线图及柱状图实现坐标显示

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 开始逐行递增。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾激光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值