IT

팬더 플롯에 x 및 y 레이블 추가

lottoking 2020. 6. 12. 08:30
반응형

팬더 플롯에 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

반응형