简体   繁体   中英

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. Every 2 columns alternately have the same number of rows. 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.

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

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.

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

2nd edit for additions: Colors and labels can be specified in the plot command with the label and color or c keyword arguments:

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. 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.

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.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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