如何在Matplotlib中为多个图形设置静态Y轴?

时间:2019-04-11 14:15:06

标签: python matplotlib

我的Python代码有问题。我将物理项目中的一些测试结果写入了csv文件中。然后,我编写了一个Python脚本,该脚本使用matplotlib在图中显示结果。基本上,我有10个不同的系列(例如位置,速度,加速度和秒),我希望它们在一个数字内。

但这是问题所在:它完全弄乱了我的Y轴值,我不知道为什么。我只希望从-11的静态Y轴,然后仅填充不同的值。 X轴很好;那是我几秒钟的时间。

enter image description here

我尝试使用plt.axisplt.axes做一些事情,但是我真的很困在这里。这是我正在谈论的代码片段:

import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as tick


positionM1 = []
positionM2 = []
velocityM1 = []
velocityM2 = []
accel1 = []
accel2 = []
sec = []
forceM1 = []
forceM2 = []
federPressed = []


with open("filename", 'r') as csvfile:
    plots= csv.reader(csvfile, delimiter=',')
    for row in plots:
        sec.append((row[0]))
        velocityM1.append((row[1]))
        velocityM2.append((row[2]))
        positionM1.append((row[3]))
        positionM2.append((row[4]))
        accel1.append((row[5]))
        accel2.append((row[6]))
        forceM1.append((row[7]))
        forceM2.append((row[8]))
        federPressed.append((row[9]))



plt.axis('normal')
plt.plot(sec, velocityM1, 'b', sec, velocityM2, 'g', sec, positionM1, 'r')
plt.plot(sec, velocityM2, label="VelocityM2")
plt.plot(sec, positionM1, label="PositionM1")
plt.plot(sec, positionM2, label="PositionM2")
plt.plot(sec, accel1, label="AccelerationM1")
plt.plot(sec, accel2, label="AccelerationM2")
plt.plot(sec, forceM1, label="Newton M1")
plt.plot(sec, forceM2, label="Newton M1")
plt.plot(sec, federPressed, label="Feder gepresst")
plt.legend(fontsize=10)
plt.grid('True')


plt.show()

这是CSV文件:

0.0000,0.5000,0.0000,0.0000,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.0250,0.5000,0.0000,0.0125,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.0500,0.5000,0.0000,0.0250,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.0750,0.5000,0.0000,0.0375,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.1000,0.5000,0.0000,0.0500,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.1250,0.5000,0.0000,0.0625,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.1500,0.5000,0.0000,0.0750,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.1750,0.5000,0.0000,0.0875,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.2000,0.5000,0.0000,0.1000,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.2250,0.5000,0.0000,0.1125,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.2500,0.5000,0.0000,0.1250,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.2750,0.5000,0.0000,0.1375,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.3000,0.5000,0.0000,0.1500,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.3250,0.5000,0.0000,0.1625,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.3500,0.5000,0.0000,0.1750,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.3750,0.5000,0.0000,0.1875,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.4000,0.5000,0.0000,0.2000,0.3000,0.0000,0.0000,0.0000,0.0000,0.0000,
0.4250,0.4500,0.0500,0.2113,0.3012,-2.0000,2.0000,-1.0000,1.0000,0.1000,
0.4500,0.4050,0.0950,0.2214,0.3036,-1.8000,1.8000,-0.9000,0.9000,0.0900,
0.4750,0.3639,0.1361,0.2305,0.3070,-1.6450,1.6450,-0.8225,0.8225,0.0822,
0.5000,0.3256,0.1744,0.2386,0.3114,-1.5311,1.5311,-0.7656,0.7656,0.0766,
0.5250,0.2892,0.2108,0.2458,0.3167,-1.4555,1.4555,-0.7278,0.7278,0.0728,
0.5500,0.2538,0.2462,0.2522,0.3228,-1.4163,1.4163,-0.7082,0.7082,0.0708,
0.5750,0.2185,0.2815,0.2576,0.3299,-1.4125,1.4125,-0.7063,0.7063,0.0706,
0.6000,0.1824,0.3176,0.2622,0.3378,-1.4440,1.4440,-0.7220,0.7220,0.0722,
0.6250,0.1446,0.3554,0.2658,0.3467,-1.5116,1.5116,-0.7558,0.7558,0.0756,
0.6500,0.1042,0.3958,0.2684,0.3566,-1.6170,1.6170,-0.8085,0.8085,0.0809,
0.6750,0.0601,0.4399,0.2699,0.3676,-1.7629,1.7629,-0.8814,0.8814,0.0881,
0.7000,0.0113,0.4887,0.2702,0.3798,-1.9528,1.9528,-0.9764,0.9764,0.0976,

3 个答案:

答案 0 :(得分:3)

@mrzo和@ user3053452很好:它们利用了简化工作的库,因此您不太容易出错。

但是让我解释一下为什么您的代码不起作用,我认为理解这一点很重要。

您忘记了将数字转换为浮点数。您当前有字符串。如果您这样做:

with open("data.txt", 'r') as csvfile:
    plots= csv.reader(csvfile, delimiter=',')
    rows = [list(map(float, line[:-1])) for line in plots]
    for row in rows:
        sec.append((row[0]))
        velocityM1.append((row[1]))
        #and so on for all the stuffs to be plotted, as in your code

您可以得到想要的东西。

让我解释一下我在此行中所做的事情:rows = [list(map(float, line[:-1])) for line in plots]。列表解析使用map内置函数将元素从csv的每一行转换为浮点型。我需要通过在行上进行切片来删除最后一个元素,并取除最后一个元素([:-1]东西)以外的所有元素,因为csv文件中的尾部逗号是空的。

这样,matplotlib可以正确解释x和y坐标,并将其放置在原本应放置的位置。

答案 1 :(得分:1)

请查看pandas模块,尤其是其DataFrame类和read_csv函数。我认为这会使您更轻松(https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)。

使用熊猫的可能解决方案:

import matplotlib.pyplot as plt
import pandas as pd

# names of the columns in the csv file
variables = [
    "time", "velocityM1", "velocityM2", "PositionM1", "PositionM2", 
    "AccelerationM1", "AccelerationM2", "Newton M1", "Newton M2",
    "Feder gepresst"
]

# Load the csv file, set the first column automatically as index (time column). 
# There is no header line in the file, therefore we have to set it to None
df = pd.read_csv('test.csv', header=None, index_col=0, names=variables)

# plot all columns and show the plot
df.plot()
plt.show()

请注意,我从csv文件的每一行中删除了结尾的逗号,以使其正常工作。

Output

答案 2 :(得分:1)

使用numpy的解决方案:

import matplotlib.pyplot as plt
import numpy as np
from numpy import genfromtxt
my_data = genfromtxt('test.csv', delimiter=',')
my_data = np.delete(my_data, -1, axis=1)
names = ['positionM1','positionM2','velocityM1','velocityM2','accel1','accel2',
         'sec','forceM1','forceM2','federPressed']

plt.axis('normal')
for i, name in zip(range(1, my_data.shape[1]), names):
    plt.plot(my_data[:,0], my_data[:,i], label=name)
plt.legend()
plt.grid(True)
plt.show()

这里的解决方案是使用genfromtext将数据从csv加载到numpy数组,然后逐列绘制它们。鉴于第一列是时间,我从第二列开始循环,因此range(1, ...)

由于您的数据末尾有,,因此我使用my_data = np.delete(my_data, -1, axis=1)删除了最后一列。

输出:

enter image description here