python 读取excel文件并画折线图

import pandas as pd
from pylab import *
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt

# Load the Excel file
#此处“\”需要转义
file_path = '文件目录\\dddd.xlsx'

# Read the Excel file
df = pd.read_excel(file_path)

# Extract the required columns for plotting
\\df['列标题']
original_x = df['x原始坐标']
predicted_x = df['x预测坐标']



# True value
true_value_x=730
true_value_y =920
true_value_d =1140
true_value_a = 14

# Create a line plot
plt.figure(figsize=(10, 6))

mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(original_x, label='x', marker='o')
plt.plot(predicted_x, label='x预测', marker='x')
plt.axhline(y=true_value_x, color='r', linestyle='-', label='真实值')

# Add labels and title
plt.xlabel('Index')
plt.ylabel('Coordinates')
plt.title('x坐标 vs 预测坐标')
plt.legend()
//保存图片
plt.savefig('./x原始vs预测.jpg')
# Display the plot
plt.show()

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python可以使用pandas库读取Excel数据,并使用matplotlib库绘制折线图。 首先需要安装pandas和matplotlib库,可以使用以下命令安装: ``` pip install pandas pip install matplotlib ``` 读取Excel数据可以使用pandas的read_excel函数,例如: ```python import pandas as pd df = pd.read_excel('data.xlsx', sheet_name='Sheet1') ``` 其中,data.xlsx为Excel文件名,Sheet1为工作表名。 读取数据后,可以使用matplotlib库绘制折线图,例如: ```python import matplotlib.pyplot as plt plt.plot(df['日期'], df['销售额']) plt.show() ``` 其中,df['日期']和df['销售额']分别为Excel表格中日期和销售额列的列名。 完整代码如下: ```python import pandas as pd import matplotlib.pyplot as plt df = pd.read_excel('data.xlsx', sheet_name='Sheet1') plt.plot(df['日期'], df['销售额']) plt.show() ``` ### 回答2: Python是一种非常流行的编程语言,广泛应用于科学计算、数据分析等领域。其中,Pandas和Matplotlib是Python中非常常用的库,可以实现对Excel数据进行读取、处理,并绘制出各种可视化图表。 在进行数据分析时,绘制折线图是一种非常常见的可视化方法。下面我们就来介绍一下Python如何读取Excel数据折线图。 1. 导入相关库 在Python中,我们需要先安装并导入Pandas和Matplotlib这两个库。 ```python import pandas as pd import matplotlib.pyplot as plt ``` 2. 读取Excel数据 使用Pandas库的read_excel()方法可以读取Excel文件中的数据,其中需要指定Excel文件的路径、sheet名称等相关参数。例如,如果我们要读取名为“data”的Excel文件中的“Sheet1”表格中的数据,可以这样写: ```python df = pd.read_excel('data.xlsx', sheet_name='Sheet1') ``` 读取后的数据会被保存在一个名为df的变量中,可以通过df.head()方法查看前几行数据。 3. 数据处理与可视化 读取Excel数据后,我们需要使用Matplotlib库来绘制折线图。 首先,我们需要使用Pandas库将数据按照需要的方式处理。比如,如果有多列数据需要绘制在同一张图上,我们需要将这些数据分别放入到不同的列中,然后使用groupby方法来按照指定的列进行分组。例如,如果我们要绘制日期对应的两列数据,可以这样处理数据: ```python df_group = df[['日期', '数据1', '数据2']].groupby('日期').mean() ``` 然后,我们可以使用Matplotlib库来绘制折线图,绘制代码如下: ```python plt.plot(df_group['数据1'], color='b', label='数据1') plt.plot(df_group['数据2'], color='r', label='数据2') plt.xlabel('日期') plt.ylabel('数据值') plt.title('数据折线图') plt.legend() plt.show() ``` 其中,plt.plot()方法可以绘制每条折线,color参数指定折线的颜色,label参数指定折线的标签(用于图例显示)。xlabel、ylabel和title分别指定x轴、y轴和图表的标题。legend()方法用于显示图例,show()方法用于显示图表。 这样,我们就可以读取Excel数据并绘制折线图了。当然,如果需要绘制其他类型的图表,也可以使用Matplotlib库中的其他方法来实现。 ### 回答3: Python读取Excel数据折线图是一项非常常见且实用的技能,很多人在日常工作、学习中都需要用到。Python读取Excel数据可以使用pandas库,而折线图则可以使用matplotlib库。 首先,我们需要在python导入所需要的库。使用pandas库来读取Excel表格中的数据,使用matplotlib库来折线图。 ```python import pandas as pd import matplotlib.pyplot as plt ``` 然后,我们要读取Excel文件数据,使用pandas库中的read_excel方法。read_excel方法接受两个必要参数,一个是文件名,一个是读取的工作表名字,此外还有一些可选参数,例如使用哪一行作为列名等。 ```python file_name = 'filename.xlsx' sheet_name = 'sheetname' df = pd.read_excel(file_name, sheet_name) ``` 接下来就可以选择所需要的数据折线图了。为了更方便地读取数据,可以将Excel表格中的日期作为DataFrame的索引。 ```python df = df.set_index('date') ``` 然后,就可以折线图了。matplotlib中最常用的是plot方法来折线图。首先,先设置一些图形的属性,例如字体、标题等。 ```python plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文显示 plt.title('折线图') ``` 然后,用plot方法折线图。plot方法会返回一个Axes对象,可以用来设置折线的颜色、线宽等。 ```python ax = df.plot(kind='line', figsize=(10,6)) ax.set_xlabel('日期') ax.set_ylabel('数据') ``` 最后,记得用show方法显示图形。 ```python plt.show() ``` 完整的代码如下: ```python import pandas as pd import matplotlib.pyplot as plt file_name = 'filename.xlsx' sheet_name = 'sheetname' df = pd.read_excel(file_name, sheet_name) df = df.set_index('date') plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文显示 plt.title('折线图') ax = df.plot(kind='line', figsize=(10,6)) ax.set_xlabel('日期') ax.set_ylabel('数据') plt.show() ``` 总之,Python读取Excel数据折线图是一项非常实用的技能,在工作、学习中都需要用到。参考以上的代码,大家可以轻松地读取Excel中的数据出自己想要的折线图

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值