Python 如何绘制数据框中存储的垂直数据集?

Python 如何绘制数据框中存储的垂直数据集?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个结构为 columns = ['curve_number', 'x', 'y1', 'y2' ] 如何使用matplotlib绘制(覆盖)所有曲线编号的x,y1和x,y2曲线 数据结构如下 curve_number x y1 y2 1 .5 3 2 1 1 5 6 1 1.5 4 3 1 2 3 7

我有一个结构为

columns = ['curve_number', 'x', 'y1', 'y2' ]
如何使用
matplotlib
绘制(覆盖)所有
曲线编号的
x,y1
x,y2
曲线

数据结构如下

curve_number   x     y1    y2
1              .5     3     2
1              1      5     6
1              1.5    4     3
1              2      3     7
2              .5     5     8
2              1      3     7
2              1.5    8     3
2              2      9     1
3              .5     2     2

事实上,我已经垂直存储了多组数据。

我不确定这是否是您想要的,但您可以尝试按
曲线编号进行分组,然后在循环中绘制每组

实际上,最复杂的部分是如何以不同的方式表示每条曲线中的各个曲线编号和
y1
y2
:在本例中,我使用颜色贴图来表示每条曲线(因此,如果曲线太多,就不会耗尽颜色,而是在渐变上),我刚刚更改了
y1
y2
之间的线型。实际上,下面代码的很大一部分只是用于设置颜色渐变和创建准确表示所有这些的图例。。。打印本身仅在
for
循环中完成

import matplotlib.pyplot as plt
# This import is just for the weird legend
from matplotlib.lines import Line2D
# This import is for the colormaps:
from matplotlib import cm

fig, axes = plt.subplots()

# Setup colormap to have right number of colors (one per number of curves)
n = df['curve_number'].nunique()
colors = cm.jet(np.linspace(0,1,n))

# Actual plotting
for i, (curve, data) in enumerate(df.groupby('curve_number')):
    plt.plot(data.x, data.y1, color = colors[i], linestyle='--', label='')
    plt.plot(data.x, data.y2, color = colors[i], label = curve)

# Regular legend (top right corner):
l1 = plt.legend(loc=1)
axes.add_artist(l1)

# Custom legend (bottom right corner):
custom_lines = [Line2D([0], [0], color='black', linestyle='--'),
                Line2D([0], [0], color='black')]    
l2 = plt.legend(custom_lines, ['y1', 'y2'], loc=4)

我不确定这是否是您想要的,但您可以尝试按
曲线编号进行分组,然后在循环中绘制每组

实际上,最复杂的部分是如何以不同的方式表示每条曲线中的各个曲线编号和
y1
y2
:在本例中,我使用颜色贴图来表示每条曲线(因此,如果曲线太多,就不会耗尽颜色,而是在渐变上),我刚刚更改了
y1
y2
之间的线型。实际上,下面代码的很大一部分只是用于设置颜色渐变和创建准确表示所有这些的图例。。。打印本身仅在
for
循环中完成

import matplotlib.pyplot as plt
# This import is just for the weird legend
from matplotlib.lines import Line2D
# This import is for the colormaps:
from matplotlib import cm

fig, axes = plt.subplots()

# Setup colormap to have right number of colors (one per number of curves)
n = df['curve_number'].nunique()
colors = cm.jet(np.linspace(0,1,n))

# Actual plotting
for i, (curve, data) in enumerate(df.groupby('curve_number')):
    plt.plot(data.x, data.y1, color = colors[i], linestyle='--', label='')
    plt.plot(data.x, data.y2, color = colors[i], label = curve)

# Regular legend (top right corner):
l1 = plt.legend(loc=1)
axes.add_artist(l1)

# Custom legend (bottom right corner):
custom_lines = [Line2D([0], [0], color='black', linestyle='--'),
                Line2D([0], [0], color='black')]    
l2 = plt.legend(custom_lines, ['y1', 'y2'], loc=4)

我们不知道这是否是OP想要的。但我喜欢这个解决方案:)+1。您可以使用曲线号作为索引而不是创建枚举来稍微简化它。应该可以在这个用例中使用。仔细想想,我实际上意识到OP会很快用我的旧方法用完颜色,所以我将其更改为在颜色贴图上绘制曲线。我们不知道这是否是OP想要的。但我喜欢这个解决方案:)+1。您可以使用曲线号作为索引而不是创建枚举来稍微简化它。在这个用例中应该可以工作。仔细想想,我实际上意识到,用我的旧方法OP会很快用完颜色,所以我把它改为在颜色贴图上绘制曲线。