英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论