두 그림을 나란히 만드는 방법
matplotlib에서 다음 예제를 찾았습니다.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()
제 질문은:플롯을 나란히 하려면 무엇을 변경해야 합니까?
하위구 설정을 다음으로 변경합니다.
plt.subplot(1, 2, 1)
...
plt.subplot(1, 2, 2)
의 매개 변수subplot
행 수, 열 수 및 현재 위치한 하위 플롯입니다.그렇게1, 2, 1
"1행, 2열 그림: 첫 번째 하위 그림으로 이동"을 의미합니다.그리고나서1, 2, 2
"1행, 2열 그림: 두 번째 하위 그림으로 이동"을 의미합니다.
현재 2행, 1열(즉, 하나는 다른 열 위에) 레이아웃을 요청하고 있습니다.대신 1행, 2열 레이아웃을 요청해야 합니다.결과는 다음과 같습니다.
하위 플롯의 중복을 최소화하기 위해 다음을 시작할 수 있습니다.
plt.tight_layout()
공연 전에산출:
이 페이지를 확인하십시오. http://matplotlib.org/examples/pylab_examples/subplots_demo.html
plt.subplots
유사합니다.수치의 파라미터를 설정하는 것이 더 쉽기 때문에 더 좋다고 생각합니다.처음 두 인수는 레이아웃(이 경우 1행, 2열)을 정의하고 그림 크기와 같은 다른 매개변수 변경 피쳐를 정의합니다.
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
axes[0].plot(x1, y1)
axes[1].plot(x2, y2)
fig.tight_layout()
하위 플롯을 한 방향으로 쌓을 때 몇 개의 축만 작성하는 경우에는 즉시 압축을 푸는 것이 좋습니다.
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(20,8))
sns.histplot(df['Price'], ax=ax1)
sns.histplot(np.log(df['Price']),ax=ax2)
plt.show()
-matplotlib.gridspec을 사용할 수 있습니다.그리드 사양
확인 - https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html
아래 코드는 열 지도를 오른쪽에 표시하고 이미지를 왼쪽에 표시합니다.
#Creating 1 row and 2 columns grid
gs = gridspec.GridSpec(1, 2)
fig = plt.figure(figsize=(25,3))
#Using the 1st row and 1st column for plotting heatmap
ax=plt.subplot(gs[0,0])
ax=sns.heatmap([[1,23,5,8,5]],annot=True)
#Using the 1st row and 2nd column to show the image
ax1=plt.subplot(gs[0,1])
ax1.grid(False)
ax1.set_yticklabels([])
ax1.set_xticklabels([])
#The below lines are used to display the image on ax1
image = io.imread("https://images-na.ssl-images- amazon.com/images/I/51MvhqY1qdL._SL160_.jpg")
plt.imshow(image)
plt.show()
기본적으로 필요한 행과 열 수를 정의해야 합니다.표시할 범주형 열이 총 4개 있다고 가정합니다.2개 행과 2개 열에 총 4개의 그림이 있다고 가정합니다.
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
sns.set_style("darkgrid")
%matplotlib inline
#15 by 15 size set for entire plots
plt.figure(figsize=(15,15));
#Set rows variable to 2
rows = 2
#Set columns variable to 2, this way we will plot 2 by 2 = 4 plots
columns = 2
#Set the plot_count variable to 1
#This variable will be used to define which plot out of total 4 plot
plot_count = 1
cat_columns = [col for col in df.columns if df[col].dtype=='O']
for col in cat_columns:
plt.subplot(rows, columns, plot_count)
sns.countplot(x=col, data=df)
plt.xticks(rotation=70);
#plot variable is incremented by 1 till 4, specifying which plot of total 4 plots
plot_count += 1
언급URL : https://stackoverflow.com/questions/42818361/how-to-make-two-plots-side-by-side
'programing' 카테고리의 다른 글
Git에 따르면 누가 "우리"이고 누가 "그들"입니까? (0) | 2023.07.01 |
---|---|
각진서비스 대 공장 (0) | 2023.07.01 |
Visual Studio Code: .git 폴더/파일 숨김 (0) | 2023.07.01 |
유형 스크립트: 메서드는 정적일 수 있습니다. (0) | 2023.07.01 |
Perl DBD:: Oracle 모듈 설치 (0) | 2023.07.01 |