英文:
Plot list of tuples (containing the graph number)
问题
I can help you translate the non-code part of your text. Here it is:
我的问题与这个问题相关:“Python:绘制元组列表”。区别在于元组的结构。第一部分显示绘图的编号,第二部分显示值(y值)。
我已经成功将数据从一个大列表中提取到了三个列表中。
如何在三个图中绘制这个元组列表(线图)?
英文:
My question is related to this one "Python: plot list of tuples". The difference lies in the structure of the tuple. The first part shows the number of the plot and the second the value (y).
I have managed to extract the data from one big list into three lists.
How can I plot this tuple-list in three plots (line plot)?
[(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
[(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
[(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
答案1
得分: 0
import matplotlib.pyplot as plt
p1 = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
p2 = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
p3 = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
def cleaner(mylist):
out = []
for tup in mylist:
newtup = tuple()
for item in tup:
newtup += (int(item),)
out.append(newtup)
return(out)
plt.plot(*zip(*cleaner(p1)))
plt.show()
英文:
It's the same, just add a function to clean the tuples to convert the strings to ints:
import matplotlib.pyplot as plt
p1 = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
p2 = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
p3 = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
def cleaner(mylist):
out = []
for tup in mylist:
newtup = tuple()
for item in tup:
newtup += (int(item),)
out.append(newtup)
return(out)
plt.plot(*zip(*cleaner(p1)))
plt.show()
答案2
得分: 0
你需要为你的元组创建一个第三维度来表示x轴,否则没有东西可供绘制。我假设这些元组是按顺序排列的,并且可以将列表中的元组索引视为一种时间元素。
我认为如果你一步一步来会更容易理解。你可以将每个元组中的第一个项目视为线条颜色,第二个项目视为y轴值。假设这些是按顺序排列的,你可以枚举每个列表并将递增的值视为你的x轴值。
因此,遍历所有3个列表,然后遍历每个枚举的元组,将x、y和颜色值附加到一个主列表data
中,这将成为你最终数据集的x、y和颜色值。
将这些数据导入到一个pandas数据框中,并使用seaborn创建你要查找的多线图。
import pandas as pd
import seaborn as sns
a = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
b = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
c = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
data = []
for x in [a, b, c]:
for i, t in enumerate(x):
data.append([i, t[0], t[1]])
df = pd.DataFrame(data)
df = df.astype(int)
df.columns = ['Time', 'Color', 'Value']
sns.lineplot(x='Time', y='Value', hue='Color', data=df)
英文:
You have to create a 3rd dimension to your tuple to represent the x axis, otherwise there's nothing to plot. I'm assuming these are in order and that the index of the tuple within the list can be treated as a sort of time element.
I think it would be easier to understand if you take it step by step. You can treat the first item in each tuple as the line color and the second as the y-axis value. Assuming these are in order, you can enumerate each list and take the incrementing value as your x-axis value.
So iterate over all 3 lists, then iterate over each enumerated tuple, appending to a master list data
what will be the x, y, and color values for your final data set.
Pull those into a pandas dataframe and use seaborn to cleanly create the multi line plot that you are looking for.
import pandas as pd
import seaborn as sns
a = [(1, '0'), (1, '4'), (1, '2'), (1, '6'), (1, '3'), (1, '4'), (1, '5'), (1, '6'), (1, '7'), (1, '12'), (1, '23')]
b = [(2, '1'), (2, '7'), (2, '2'), (2, '4'), (2, '1'), (2, '7'), (2, '2'), (2, '3'), (2, '4'), (2, '2'), (2, '3')]
c = [(3, '2'), (3, '4'), (3, '4'), (3, '2'), (3, '1'), (3, '13'), (3, '9'), (3, '8'), (3, '4'), (3, '2'), (3, '7')]
data = []
for x in [a,b,c]:
for i, t in enumerate(x):
data.append([i, t[0], t[1]])
df = pd.DataFrame(data)
df = df.astype(int)
df.columns=['Time','Color','Value']
sns.lineplot(x='Time',y='Value',hue='Color',data=df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论