简体   繁体   English

使用python在同一图中为csv dile绘制多个x轴和多个y轴

[英]Plotting multiple x axis and multiple y axis in same graph for csv dile using python

I am having a csv file with 10 columns.我有一个包含 10 列的csv文件。 Every 2 columns alternately have the same number of rows.2 列交替具有相同的行数。 All the odd columns represents time, and even columns represents energy.所有奇数列代表时间,偶数列代表能量。

I want to plot column1, column2 together, column3, column4 together, column5, column6 together, column7, column8 together, column9, column10 together on the same plot.我想将column1、column2 一起绘制、column3、column4 一起绘制、column5、column6 一起绘制、column7、column8 一起绘制、column9、column10 一起绘制在同一个图上。

How can I do this?我怎样才能做到这一点?

For example.例如。

sample.csv

1 99 2 98 1 98 3 99 ...
2 98 3 97 2 97 4 98 ...
3 97 4 96 3 96 5 97 ...
     5 95 4 95 6 96 ...
               7 95 ...
               8 94 ...

With an artificial input test.csv, like使用人工输入 test.csv,例如

a,b,c,d
1,50,2,20
2,60,3,40
,,4,60

this code worked for me and produced one image with two graphs that represent columns 1+2 and 3+4.这段代码对我有用,并生成了一张带有两个图形的图像,分别代表第 1+2 和 3+4 列。

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("test.csv")

for i in range(0,len(df.columns),2):
    plt.plot(df.iloc[:,i].values, df.iloc[:,i+1].values)
plt.show()

Edit: Initially, worked only for 4 columns, should work for even more now编辑:最初,仅适用于 4 列,现在应该适用更多

2nd edit for additions: Colors and labels can be specified in the plot command with the label and color or c keyword arguments:添加的第二次编辑:可以在 plot 命令中使用labelcolor or c关键字参数指定颜色和标签:

color_list =["blue", "red", "green"]

for i in range(0,len(df.columns),2):
    plt.plot(df.iloc[:,i].values, df.iloc[:,i+1].values, label = df.columns[i+1], color = color_list[i//2])
plt.legend()
plt.show()

this works if the labels are given as the top line in the csv file and are included in the dataframe.如果标签作为 csv 文件中的第一行给出并且包含在数据框中,则此方法有效。 Alternatively, you can specify a custom list just like I did for the colors.或者,您可以像我为颜色所做的那样指定一个自定义列表。 There are more complex but also convenient ways for setting the color, eg color maps and sets, but I guess this is the easiest solution.有更复杂但也更方便的设置颜色的方法,例如颜色映射和设置,但我想这是最简单的解决方案。 More information and alternative implementations can be found here for labels and here for colors .更多信息和替代实现可以在这里找到标签颜色 In general, the matplotlib documentation is very extensive and easy to read.一般来说,matplotlib 文档非常广泛且易于阅读。

Not sure if you want all plots in one image or separately for each pair of columns.不确定您是要在一个图像中还是单独为每对列绘制所有图。 Here is a solution where you can display easily each pair of column using a function.这是一个解决方案,您可以使用函数轻松显示每对列。

Modules模块

import io
import pandas as pd
import matplotlib.pyplot as plt

Data数据

df = pd.read_csv(io.StringIO("""
1 99 2 98 1 98 3 99 ...
2 98 3 97 2 97 4 98 ...
3 97 4 96 3 96 5 97 ...
     5 95 4 95 6 96 ...
               7 95 ...
               8 94 ...
"""), delim_whitespace=True, header=None, columns=[], engine="python")

Function where you need to put in x , the first column, it adds then as Y-axis the column that is next.您需要输入x第一列的x ,然后将下一列添加为 Y 轴。

def plotfunction(x):
    plt.plot(df.iloc[:,x], df.iloc[:,x+1])
    plt.show()

plotfunction(0)

Use for multiple plots the following.用于多个绘图以下。

for i in range(4):
    plotfunction(i)

Or in nicer subplot.或者在更好的子情节中。

fig = plt.figure(figsize=(10, 6))

for i,x in zip([1,2,3,4], [0,2,4,6]):
    ax = fig.add_subplot(2,2,i)
    ax.plot(df.iloc[:,x], df.iloc[:,x+1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM