Как установить положение заголовка внутри графика на точечной диаграмме? - PullRequest
0 голосов
/ 04 ноября 2018

MWE: Я хотел бы, чтобы позиция заголовка была такой же, как на графике: enter image description here

Вот мой код:

import matplotlib.pyplot as plt
import numpy as np
import random 


fig, ax = plt.subplots()

x = random.sample(range(256),200)
y = random.sample(range(256),200)

cor=np.corrcoef(x,y)

plt.scatter(x,y, color='b', s=5, marker=".")
#plt.scatter(x,y, label='skitscat', color='b', s=5, marker=".")
ax.set_xlim(0,300)
ax.set_ylim(0,300)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Correlation Coefficient: %f'%cor[0][1])
#plt.legend()
fig.savefig('plot.png', dpi=fig.dpi)
#plt.show()

Но это дает:
enter image description here

Как мне исправить эту позицию заголовка?

Ответы [ 2 ]

0 голосов
/ 04 ноября 2018

Будет излишне сложно перемещать title в произвольное положение внутри осей.
Вместо этого лучше создать text в желаемой позиции.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

x = np.random.randint(256,size=200)
y = np.random.randint(256,size=200)

cor=np.corrcoef(x,y)

ax.scatter(x,y, color='b', s=5, marker=".")

ax.set_xlim(0,300)
ax.set_ylim(0,300)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.text(0.9, 0.9, 'Correlation Coefficient: %f'%cor[0][1], 
        transform=ax.transAxes, ha="right")

plt.show()

enter image description here

0 голосов
/ 04 ноября 2018

используйте этот код:

x= [2,1]; y = [3,2]
plt.scatter(x,y)
plt.title("title", y=0.5)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
...