Matplotlib多行堆栈

时间:2019-02-21 19:49:00

标签: python matplotlib

我有4个.csv文件,其中包含一些原始数据(流速与力的关系),并且我试图将所有4条线放到同一张图表上。我知道这很麻烦,但这是我的代码:

import csv
import numpy as np
import matplotlib.pylab as plt

with open('C:\\Users\\Christopher\\Documents\\School\\2019\\Winter 2019\\ME 360\\Unit 4\\Lab 4\\Raw1.csv', newline='') as csvfile1:
    readcsv = csv.reader(csvfile1, delimiter=',')
    for row in readcsv:
        if readcsv.line_num == 1:
            flow1 = row
        if readcsv.line_num == 2:
            force1 = row

with open('C:\\Users\\Christopher\\Documents\\School\\2019\\Winter 2019\\ME 360\\Unit 4\\Lab 4\\Raw2.csv', newline='') as csvfile2:
    readcsv = csv.reader(csvfile2, delimiter=',')
    for row in readcsv:
        if readcsv.line_num == 1:
            flow2 = row
        if readcsv.line_num == 2:
            force2 = row


with open('C:\\Users\\Christopher\\Documents\\School\\2019\\Winter 2019\\ME 360\\Unit 4\\Lab 4\\Raw3.csv', newline='') as csvfile3:
    readcsv = csv.reader(csvfile3, delimiter=',')
    for row in readcsv:
        if readcsv.line_num == 1:
            flow3 = row
        if readcsv.line_num == 2:
            force3 = row


with open('C:\\Users\\Christopher\\Documents\\School\\2019\\Winter 2019\\ME 360\\Unit 4\\Lab 4\\Raw4.csv', newline='') as csvfile4:
    readcsv = csv.reader(csvfile4, delimiter=',')
    for row in readcsv:
        if readcsv.line_num == 1:
            flow4 = row
        if readcsv.line_num == 2:
            force4 = row


# Change plt font family and text size
plt.rc('font', family='serif', size=10)

# Make the plot
plt.figure(figsize=(5,3))
plt.plot(flow1,force1,'ro--')
plt.plot(flow2,force2,'bs--')
plt.plot(flow3,force3,'g^--')
plt.plot(flow4,force4,'y*--')

# Format the plot
plt.xlabel('Flow Rate (L/min)')
plt.ylabel('Force (N)')
plt.legend(['2.83 mm Nozzle','3.28 mm Nozzle','5.25 mm Nozzle','6.35 mm Nozzle'],loc='lower right')
plt.tight_layout()
#plt.axis([0,10,0,1])
plt.show()

令人惊讶的是,输出是... 4 Lines Stacked On Top of Each Other

我如何让所有4条线使用相同的y轴(流速),而不是将每条线堆叠在另一条顶部?

0 个答案:

没有答案