팬더 플롯에 x 및 y 레이블 추가
팬더를 사용하여 매우 간단한 것을 그리는 다음 코드가 있다고 가정합니다.
import pandas as pd
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
df2.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Video streaming dropout by category')

특정 컬러 맵을 사용하는 능력을 유지하면서 x 및 y 레이블을 쉽게 설정하려면 어떻게합니까? 나는 것으로 나타났습니다 plot()팬더 DataFrames에 대한 래퍼가에 대한 매개 변수의 특정을지지 않습니다.
이 df.plot()함수는 matplotlib.axes.AxesSubplot객체를 반환 합니다. 해당 객체에 레이블을 설정할 수 있습니다.
In [4]: ax = df2.plot(lw=2, colormap='jet', marker='.', markersize=10, title='Video streaming dropout by category')
In [6]: ax.set_xlabel("x label")
Out[6]: <matplotlib.text.Text at 0x10e0af2d0>
In [7]: ax.set_ylabel("y label")
Out[7]: <matplotlib.text.Text at 0x10e0ba1d0>

또는 더 간결하게 : ax.set(xlabel="x label", ylabel="y label").
또는 인덱스 x 축 레이블이 인덱스 이름 (있는 경우)으로 자동 설정됩니다. 그래서 df2.index.name = 'x label'작동합니다.
다음과 같이 사용할 수 있습니다.
import matplotlib.pyplot as plt
import pandas as pd
plt.figure()
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
df2.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Video streaming dropout by category')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.show()
분명히 문자열 'xlabel'과 'ylabel'을 원하는 것으로 바꾸어야합니다.
DataFrame의 열과 색인에 레이블을 지정하면 팬더가 자동으로 적절한 레이블을 제공합니다.
import pandas as pd
values = [[1, 2], [2, 5]]
df = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
df.columns.name = 'Type'
df.index.name = 'Index'
df.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Video streaming dropout by category')

In this case, you'll still need to supply y-labels manually (e.g., via plt.ylabel as shown in the other answers).
It is possible to set both labels together with axis.set function. Look for the example:
import pandas as pd
import matplotlib.pyplot as plt
values = [[1,2], [2,5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'], index=['Index 1','Index 2'])
ax = df2.plot(lw=2,colormap='jet',marker='.',markersize=10,title='Video streaming dropout by category')
# set labels for both axes
ax.set(xlabel='x axis', ylabel='y axis')
plt.show()
For cases where you use pandas.DataFrame.hist:
plt = df.Column_A.hist(bins=10)
Note that you get an ARRAY of plots, rather than a plot. Thus to set the x label you will need to do something like this
plt[0][0].set_xlabel("column A")
pandas uses matplotlib for basic dataframe plots. So, if you are using pandas for basic plot you can use matplotlib for plot customization. However, I propose an alternative method here using seaborn which allows more customization of the plot while not going into the basic level of matplotlib.
Working Code:
import pandas as pd
import seaborn as sns
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
ax= sns.lineplot(data=df2, markers= True)
ax.set(xlabel='xlabel', ylabel='ylabel', title='Video streaming dropout by category')
what about ...
import pandas as pd
import matplotlib.pyplot as plt
values = [[1,2], [2,5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'], index=['Index 1','Index 2'])
(df2.plot(lw=2,colormap='jet', marker='.', markersize=10, title='Video streaming dropout by category')
.set(xlabel='x axis', ylabel='y axis')
)
plt.show()
참고URL : https://stackoverflow.com/questions/21487329/add-x-and-y-labels-to-a-pandas-plot
'IT' 카테고리의 다른 글
| codeigniter 활성 레코드에서 삽입 쿼리 후 마지막 삽입 ID를 얻는 방법 (0) | 2020.06.12 |
|---|---|
| HTML 형식으로 PUT 메소드 사용 (0) | 2020.06.12 |
| Docker : 상위 디렉토리에서 파일 추가 (0) | 2020.06.12 |
| 문자열 바로 JavaScript를 자릅니다 (0) | 2020.06.12 |
| VSCode에서 선택한 단어를 모두 선택하십시오. (0) | 2020.06.12 |

