Matplotlib Line Plot이 레이블을 나타내지 않음
선 플롯을 얻고 싶은 다음 데이터 세트가 있습니다. 줄거리는 정확하지만 코드에 레이블 이름을 제공했지만 레이블이 없습니다. 라벨을 포함 할 방법을 알려주세요. 또한 코드에 xlabel과 ylabel을 포함하려고하면 오류가 발생합니다.AttributeError: 'Line2D' object has no property 'xlabel'
데이터 프레임 해상도
UserId | date |-7|-6|-5|-4|-3|-2|-1|0 |1 |2 |3 |4 |5 |6 |7
1 2009-10-17 17:38:32.590 |0 |0 |0 |0 |0 |0 |1 |0 |1 |0 |0 |0 |0 |0 |0
2 2009-10-19 00:37:23.067 |0 |0 |0 |0 |0 |1 |1 |0 |1 |0 |0 |0 |0 |0 |0
3 2009-10-20 08:37:14.143 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0
4 2009-10-21 18:07:51.247 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0
5 2009-10-22 21:25:24.483 |0 |0 |0 |0 |0 |0 |1 |0 |0 |0 |0 |0 |0 |0 |0
암호
badges = ["A", "B", "C"]
for badge in badges:
res.iloc[:,2:].mean().plot(kind='line', label = badge)
산출
이 코드에서 얻은 출력입니다. 세 선의 레이블이 모두 그래프에 표시되기를 원합니다. 이와 함께 축에 xlabel = "Week"및 ylabel = "Mean Posts"를 추가하고 싶습니다.
답변
디스플레이 전설에 필요한 기본 기능 및 축 라벨은 plt.legend, plt.xlabel및 plt.ylabel. 자체 포함 된 예는 아래를 참조하세요. 코드의 모든 관련 부분을 보지 않고 더 구체적으로 말하기는 어렵습니다.
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [4, 5, 6]
y2 = [7, 8, 9]
plt.plot(x, y1, "b", label="y1")
plt.plot(x, y2, "r", label="y2")
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.savefig("temp")
당신과 같은 모든 매개 변수 유지할 수 있습니다 plt.legend(), plt.xlabel, plt.ylabel, plt.xticks외부 등 for루프를.
이 시도:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
badges = ["A", "B", "C"]
for badge in badges:
res.iloc[:,2:].mean().plot(kind='line', label = badge)
plt.ylabel('y',size=20,weight='bold')
plt.xlabel('x',size=20,weight='bold')
plt.legend(frameon=False, prop={'size':15})
plt.show()
각 3 별도의 플롯을 좋아하는 경우 또한, badge다음에서 섹션을 이동 plt.ylabel()하는 plt.show()루프 내부.
xlabel 또는 ylabel 인수는 pandas 버전 1.1.0에서만 사용할 수 있습니다. 이상. 문서 참조https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html#pandas.DataFrame.plot
당신이 얻고있는 이유 AttributeError를 'Line2D를'객체 프로퍼티 'xlabel을'이 없다는 당신이 팬더의 낮은 버전이있을 수 있기 때문이다.
다음 명령을 실행하여 pandas 버전을 확인할 수 있습니다.
import pandas as pd
print(pd.__version__)
이제 x_label / y_label을 넣으려면 다음을 수행 할 수 있습니다.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for badge in badges:
res.iloc[:,2:].mean().plot(kind='line', label = badge, ax=ax)
ax.set_ylabel('y')
ax.set_xlabel('x')
ax.legend()
plt.show()