python多条曲线呈现在一张图
时间: 2024-02-22 08:57:31 浏览: 87
你可以使用Matplotlib库中的pyplot模块来实现多条曲线在一张图中呈现。
以下是一份示例代码:
```python
import matplotlib.pyplot as plt
# 准备数据
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
y3 = [5, 4, 3, 2, 1]
# 绘制图像
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.plot(x, y3, label='Line 3')
# 添加标签和标题
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Lines in One Figure')
# 添加图例
plt.legend()
# 显示图像
plt.show()
```
在这个示例中,我们定义了三条曲线的数据(y1、y2和y3),并通过plt.plot()方法在同一张图上绘制了它们。我们还添加了标签、标题和图例,最后使用plt.show()方法显示图像。
你可以根据自己的需求修改代码和数据,以便绘制出符合你要求的图像。
相关问题
qcustomplot 一个坐标多条曲线
### 使用 QCustomPlot 实现多条曲线绘制
为了实现在同一个坐标系内绘制多条曲线的功能,可以利用 `QCustomPlot` 库中的多个图形对象 (`QCPGraph`) 来表示不同的数据集。下面是一个完整的 Python 示例来展示这一过程:
```cpp
#include "qcustomplot.h"
#include <vector>
void setupMultiLinePlot(QCustomPlot *customPlot)
{
// 添加三条不同颜色的数据线到 plot 对象中
customPlot->addGraph();
customPlot->graph(0)->setPen(QPen(Qt::blue)); /* 设置线条的颜色 */
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->addGraph();
customPlot->graph(2)->setPen(QPen(Qt::green));
std::vector<double> x, y1, y2, y3;
// 填充示例数据点 (这里仅作为演示用途)
for(int i=0;i<257;++i){
double t=i/256.0*8*M_PI;
x.push_back(t);
y1.push_back(sin(t)*exp(-t*t*0.01));// 蓝色正弦波形衰减信号
y2.push_back(cos(t)*exp(-t*t*0.01)+sin(t)/2); // 红色余弦波形加部分正弦分量
y3.push_back(exp(-t*t*0.01)*(cos(t)-sin(t))); // 绿色指数调制的差频振荡器输出
}
// 将数据绑定至对应的 graph 上
customPlot->graph(0)->setData(x.data(),y1.data());
customPlot->graph(1)->setData(x.data(),y2.data());
customPlot->graph(2)->setData(x.data(),y3.data());
// 自动调整轴的比例以适应所有添加的数据
customPlot->rescaleAxes(true)[^3];
// 刷新界面以便立即看到变化效果
customPlot->replot();
}
```
这段代码展示了如何创建三个具有不同特性的函数图像并将其呈现在同一张图表之上。通过调用 `addGraph()` 方法为每组新加入的数据分配一个新的 `QCPGraph` 实例,并且可以通过设置画笔属性来自定义各条曲线的表现形式。
最后一步非常重要——即调用了 `rescaleAxes()` 函数,这确保了所有的数据都能被适当地显示出来而不会超出视窗边界。
碳排放总量呈现“西移”趋势python
### 使用Python进行碳排放总量“西移”趋势的数据分析与可视化
#### 数据获取与预处理
为了研究全球范围内碳排放总量的“西移”趋势,首先需要收集来自不同国家和地区的历史碳排放数据。这里可以利用`pandas`库读取公开可用的数据集,并对其进行初步清理。
```python
import pandas as pd
@st.cache
def get_data():
url_1 = 'https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Climate%20change%20impacts/Climate%20change%20impacts.csv'
url_2 = "https://github.com/owid/co2-data/raw/master/owid-co2-data.csv"
df_impact = pd.read_csv(url_1)
df_co2 = pd.read_csv(url_2)
# 进一步筛选和清洗数据...
return df_impact, df_co2
```
对于具体的时间范围以及地理区域的选择,则取决于实际的研究需求[^3]。
#### 地理空间分布图绘制
要直观展现这种“西移”的现象,可以通过创建地图形式的地图来表示各个国家每年度的人均二氧化碳排放量变化情况。这需要用到支持地理位置编码功能的强大绘图库如Plotly Express:
```python
import plotly.express as px
# 假设已经得到了整理好的年度各国总排放数据框df_emissions
fig = px.choropleth(
data_frame=df_emissions,
locations="iso_code", # ISO country codes
color="co2_per_capita",
hover_name="country",
animation_frame="year",
title="Global CO2 Emissions Per Capita Over Time (tons)"
)
fig.show()
```
上述代码片段将会生成一张随时间演变的世界地图,在其中可以看到颜色深浅代表了不同地区人均CO₂排放浓度的变化状况,从而帮助观察是否存在明显的向西方转移的趋势。
#### 时间序列对比分析
除了静态的地图之外,还可以构建多条折线图来进行更细致的时间维度上的比较。比如选取几个典型的东西方大国作为样本对象,分别画出它们历年来的累计排放曲线,以此评估相对位置关系是否发生了显著改变。
```python
countries_of_interest = ["China", "United States", "India", "Germany"]
filtered_df = df_emissions[df_emissions['country'].isin(countries_of_interest)]
line_fig = px.line(filtered_df, x='year', y='cumulative_total', color='country')
line_fig.update_layout(title_text='Cumulative Total Carbon Emission by Selected Countries over Years')
line_fig.show()
```
通过这种方式能够更加清晰地看出某些特定时间段内东方经济体相对于西方发达国家的增长速度差异,进而支撑起关于“西移”模式存在的论断[^1]。
阅读全文
相关推荐















