Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python绘图图例键格式_Python_Matplotlib - Fatal编程技术网

Python绘图图例键格式

Python绘图图例键格式,python,matplotlib,Python,Matplotlib,我试图用python绘制一组数据。我正在使用matplotlib.pyplot库来执行此操作 我让一切正常,但我有一个问题,传奇中的最后一把钥匙的格式比其他的都奇怪。我试着做不同的设置来修复这个问题,但到目前为止运气不好。。。有人能帮我修复CH4(A)以与其他键相同的格式显示吗 图中的格式与此非常相似(在获得10个声誉之前,我无法发布图片) CH1(A) CH2(A) CH3(A) 甲烷 (一) ) 这是我设置图例的代码 legends = data[16] legends = legends.

我试图用python绘制一组数据。我正在使用matplotlib.pyplot库来执行此操作

我让一切正常,但我有一个问题,传奇中的最后一把钥匙的格式比其他的都奇怪。我试着做不同的设置来修复这个问题,但到目前为止运气不好。。。有人能帮我修复CH4(A)以与其他键相同的格式显示吗

图中的格式与此非常相似(在获得10个声誉之前,我无法发布图片)

CH1(A)

CH2(A)

CH3(A)

甲烷

(一)

)

这是我设置图例的代码

legends = data[16]
legends = legends.split(",")

if numChannels == 1:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
elif numChannels == 2:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
elif numChannels == 3:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
elif numChannels == 4:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
    plt.plot(raw[:,0],raw[:,4], label=legends[4] + " (" + units[4] + ")")
plt.legend(loc=0)
plt.xlabel(legends[0])
plt.ylabel("Data")
plt.title(filestr)
plt.show()

图例是我从.csv文件中读取的数据。我不确定为什么会发生这种情况。这种情况只发生在最后一个图例上。例如,如果我有4个数据集和3个绘图,结果很好,但是如果我全部绘图4个数据集或只有1个数据集和绘图,则键的格式与上面所示的不同。

我想你是
单位[4]
数据可能包含一些换行符(
\n
),这些换行符会导致奇怪的图例格式。如果手动设置图例和单位字符串,则会得到正确的格式

import numpy as np
import matplotlib.pyplot as plt

raw = np.random.random((20,5))
legends = [" ", "CH1 ","CH2 ","CH3 ","CH4 "]
units = [" ", "A","A","A","A"]
numChannels = 4

if numChannels == 1:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
elif numChannels == 2:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
elif numChannels == 3:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
elif numChannels == 4:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
    plt.plot(raw[:,0],raw[:,4], label=legends[4] + " (" + units[4] + ")")
plt.legend(loc=0)
plt.xlabel(legends[0])
plt.ylabel("Data")
plt.show()
这就给了,,

我认为您的
单位[4]
数据可能包含一些换行符(
\n
),这些换行符会导致奇怪的图例格式。如果您手动设置图例和单位字符串,您会得到看起来正确的格式

import numpy as np
import matplotlib.pyplot as plt

raw = np.random.random((20,5))
legends = [" ", "CH1 ","CH2 ","CH3 ","CH4 "]
units = [" ", "A","A","A","A"]
numChannels = 4

if numChannels == 1:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
elif numChannels == 2:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
elif numChannels == 3:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
elif numChannels == 4:
    plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")")
    plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")")
    plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")")
    plt.plot(raw[:,0],raw[:,4], label=legends[4] + " (" + units[4] + ")")
plt.legend(loc=0)
plt.xlabel(legends[0])
plt.ylabel("Data")
plt.show()
这就给了,,

请给出一个演示的最小代码。我多放了一点代码。这就是我所有用于绘图的代码。我其余的代码只是用于解析数据。另外,无论我是使用从文件读取的数据还是使用硬编码的字符串变量,都无所谓。请给出一个演示的最小代码。我多放了一点代码。这是关于al的l我用于绘图的代码。我的其余代码仅用于分析数据。此外,无论我使用从文件中读取的数据还是使用硬编码的字符串变量,都无关紧要。请给出一个演示性的最小代码。我添加了更多的代码。这就是我用于绘图的所有代码。我的其余代码仅用于分析数据。此外,它也不起作用不管我是使用从文件中读取的数据还是使用硬编码的字符串变量。谢谢!我忘记了csv文件末尾的空格,这导致了奇怪的格式设置。我所要做的就是在拆分之前剥离!谢谢!我忘记了csv文件末尾的空格,这导致了奇怪的格式设置。我只知道我要做的是在分割之前先剥离!谢谢!我忘记了csv文件末尾的空格,这导致了奇怪的格式。我所要做的就是在分割之前剥离!