在单线图中绘制不同数据框。

huangapple go评论76阅读模式
英文:

Plot Different Data Frame in Single Line Plot

问题

I have Different Data Frame with same length let's say 10 rows with different values. The First Data frame is Static, however rest of them are user input let's say user want 2 data frame then total it would be three data frame with same length. I would like to plot values for all three of them in one line graph in python.

import pandas as pd
import matplotlib.pyplot as plt

L1 = pd.DataFrame([20,19,18,17,16,15])
K1 = pd.DataFrame([15,14,13,11,18,21])
k2 = pd.DataFrame([10,15,16,21,22,25])

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(L1[0])
ax1.plot(K1[0])
ax1.plot(k2[0])
fig1.savefig(Path + "abc.png")

I tried above but it just plot only L1 and K1 when I run it in a loop. How can I plot all three data frames in one figure?

英文:

I have Different Data Frame with same length let's say 10 rows with different values. The First Data frame is Static, however rest of them are user input let's say user want 2 data frame then total it would be three data frame with same length. I would like to plot values for all three of them in one line graph in python.

import pandas as pd
import matplotlib.pyplot as plt

L1 = pd.DataFrame([20,19,18,17,16,15])
K1 = pd.DataFrame([15,14,13,11,18,21])
k2 = pd.DataFrame([10,15,16,21,22,25])

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(L1[0])
ax1.plot(k1[0])
ax1.plot(k2[0])
fig1.savefig(Path + "abc.png")

I tried above but it just plot only L1 and K1 when I run it in a loop. How can I plot all three data frames in one figure?

答案1

得分: 0

当你创建一个子图时,设置行数和列数 plt.subplots(nbrow, nbcol),这将返回两个对象:主框架的图和轴,每个轴都是一个子图。

import pandas as pd
import matplotlib.pyplot as plt

L1 = pd.DataFrame([20,19,18,17,16,15])
K1 = pd.DataFrame([15,14,13,11,18,21])
k2 = pd.DataFrame([10,15,16,21,22,25])
fig, axs = plt.subplots(3, 1)
axs[0].plot(L1.values)
axs[1].plot(K1.values)
axs[2].plot(k2.values)
axs[0].set_ylabel('YLabel 0')
axs[1].set_ylabel('YLabel 1')
axs[2].set_ylabel('YLabel 2')
fig.align_ylabels()
plt.show()
英文:

when you create a subplots set number of rows and columns plt.sublots(nbrow, nbcol)
this will return two objects: figure and axis of main frame each axis being a subplot

import pandas as pd
import matplotlib.pyplot as plt


L1 = pd.DataFrame([20,19,18,17,16,15])
K1 = pd.DataFrame([15,14,13,11,18,21])
k2 = pd.DataFrame([10,15,16,21,22,25])
fig, axs = plt.subplots(3, 1)
axs[0].plot(L1.values)
axs[1].plot(K1.values)
axs[2].plot(k2.values)
axs[0].set_ylabel('YLabel 0')
axs[1].set_ylabel('YLabel 1')
axs[2].set_ylabel('YLabel 2')
fig.align_ylabels()
plt.show()

答案2

得分: 0

自从你只有数字数据,我认为使用pandas的DataFrame没有必要,所以我将它们转换为numpy数组。接下来只需要在单个子图中绘制这些数组即可。

import matplotlib.pyplot as plt
import numpy as np

L1 = np.array([20,19,18,17,16,15])
K1 = np.array([15,14,13,11,18,21])
k2 = np.array([10,15,16,21,22,25])

fig, ax = plt.subplots()
ax.plot(L1, label="L1")
ax.plot(K1, label="K1")
ax.plot(k2, label="k2")
ax.legend()
ax.set_xlabel("index")

在单线图中绘制不同数据框。

英文:

Since you just have numbers, I don't see the reason to use a pandas DataFrame, so I converted them to numpy arrays. Then it's just a matter of plotting each of the arrays in a single subplot.

import matplotlib.pyplot as plt
import numpy as np

L1 = np.array([20,19,18,17,16,15])
K1 = np.array([15,14,13,11,18,21])
k2 = np.array([10,15,16,21,22,25])

fig, ax = plt.subplots()
ax.plot(L1, label="L1")
ax.plot(K1, label="K1")
ax.plot(k2, label="k2")
ax.legend()
ax.set_xlabel("index")

在单线图中绘制不同数据框。

huangapple
  • 本文由 发表于 2023年7月12日 22:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定