我正在绘制两个条件,并且只想要两个图例。但是我的数据中有副本,并且每个副本都有一个单独的图例。为什么?如果之前已经解决了这个问题,我很抱歉,但我在这个问题上花费了大量令人尴尬的时间,而且我发现的许多东西对我的情况来说似乎过于复杂。任何帮助都将不胜感激。
import matplotlib.pyplot as plt
import pandas as pd
#####read and organize data
alldata = pd.read_csv('Fig_1.csv')
CondtionA = list(zip(alldata.iloc[:,1],alldata.iloc[:,2]))
ConditionB = list(zip(alldata.iloc[:,7],alldata.iloc[:,8]))
### make the figure
fig, ax = plt.subplots()
plt.plot(alldata['Temperature'],ConditionA,linewidth = 1,c='k', linestyle = '--',label = 'ConditionA')
plt.plot(alldata['Temperature'],ConditionB,linewidth = 1,c='k', label = "ConditonB")
ax.legend(numpoints=1)
plt.show()
发布于 2018-06-22 04:27:10
如果您删除
ax.legend(numpoints=1)
并添加
plt.legend(handles=[p1,p2], bbox_to_anchor=(0.75, 1), loc=2, borderaxespad=0.)
你将只会得到一个图例。
因此,您的代码将如下所示
import matplotlib.pyplot as plt
import pandas as pd
#####read and organize data
alldata = pd.read_csv('Fig_1.csv')
CondtionA = list(zip(alldata.iloc[:,1],alldata.iloc[:,2]))
ConditionB = list(zip(alldata.iloc[:,7],alldata.iloc[:,8]))
### make the figure
fig, ax = plt.subplots()
p1 = plt.plot(alldata['Temperature'],ConditionA,linewidth = 1,c='k', linestyle = '--',label = 'ConditionA')
p2 = plt.plot(alldata['Temperature'],ConditionB,linewidth = 1,c='k', label = "ConditonB")
#ax.legend(numpoints=1)
plt.legend(handles=[p1,p2], bbox_to_anchor=(0.75, 1), loc=2, borderaxespad=0.)
plt.show()
https://stackoverflow.com/questions/50976260
复制相似问题