For starters, your code won’t run anywhere.
Why?
Because it has numerous errors - did not import the library, did not list the points, etc.
Faced with these problems, I decided to implement a practical graphic example scatter
using the library matplotlib
, to be at your disposal as well as at the disposal of any user of the platform who may need.
Observing
Charts of the type Scatter, are graphs that display dots. These dots are intersections between lines verticais
and horizontais
- identical to the Cartesian Plan.
Example
Create a scatter chart that has 5
points with the help of matplotlib library.
To solve this example we can use the following script:
import matplotlib.pyplot as plt
titulo = input('Digite o título do gráfico: ')
legenda = input('Digite a legenda do gráfico: ')
n = int(input('Desejas inserir quantos pontos? '))
x = list()
y = list()
for c in range(1, n + 1):
pontos = list(map(int, input(f'Digite as coordenadas do {c}º ponto: ').split()))
x.append(pontos[0])
y.append(pontos[1])
fp = input('Digite a forma dos pontos: ')
tp = int(input('Digite o tamanho dos pontos: '))
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.scatter(x, y, label=f'{legenda}', color='k', s=tp, marker=f'{fp}')
plt.xlabel('eixo X', fontsize=15)
plt.ylabel('eixo y', fontsize=15)
plt.title(f'{titulo}\n', fontsize=20)
plt.legend(fontsize=10)
plt.show()
Note that when we run the script we receive the following message: Digite o título do gráfico:
. Right now we must enter the title Intersecções de pontos
and press enter
. Then we received the second message: Digite a legenda do gráfico:
. Right now we must type in the caption Pontos
and press enter
.
As this script is more generalist, the same will ask us the amount of points and at this time we must type 5
and press enter
. Then we will receive the following message: Digite as coordenadas do 1º ponto:
. At this point we must enter the two point coordinates (x, y), na same line, separated for a single space and press enter
. Then we must repeat operations for others 4
points. Subsequently, we receive the following message: Digite a forma dos pontos:
. Right now we must press a key from which we want to use the corresponding character to display the dot - by pressing the X
, the dot will be displayed as a xis. Later we will receive the following message: Digite o tamanho dos pontos:
. Right now we must type 40
and press enter
.
After we have fed all the variables of the graph matplotlib will process the data and then display the same.
Remarks
1. Graph processing will take a few seconds, according to your machine hardware;
2. The command line of the code, which in fact will control the screen size that displays the chart is:
plt.figure(figsize=(10, 6))
The value 10 corresponds to largura
and the value 6 corresponds to altura
. If you want to change these dimensions, just change the numerical values.
Very good explanation!!! Thank you! Now if I want to change the size of the legend inside the scatterplot is it possible? I am not finding the parameters in the documentation
plt.figure(figsize=(9,7)) # definindo tamanho do gráfico
plt.scatter(X,Y,label='Y(X)'); # como alterar o tamanho dessa legenda dentro o gráfico?
plt.xlabel('Eixo X'); # legenda do eixo X
plt.ylabel('Eixo Y'); # legenda do eixo 
plt.legend();
plt.show();
– LpCoutinho
@Lpcoutinho, If the answer solved your problem, you can
aceitá-la
, by clicking onV
next to the answer. See here why accept. Although not mandatory, this practice is encouraged on the site, indicating to future visitors that such a response solved the problem. And when you get 15 points, you can also vote in any useful responses.– Solkarped
@Lpcoutinho, to change the size of the caption just change the value of
fontsize
on the lineplt.legend(fontsize=10)
. You can change it to 20, 30, 40, or 5, 8, etc.– Solkarped
@Lpcoutinho, if you want to change the size of
fontsize
of the labelplt.xlabel('eixo X', fontsize=15)
andplt.ylabel('eixo y', fontsize=15)
, just also change the numerical values to whatever you want.– Solkarped
@Solkarped, in
label=f'{legenda}'
can be done directlylabel=legenda
the same thing in themarker=f'{fp}'
. There is no need for this formatting. Hug!– lmonferrari
@Lpcoutinho,
legenda
is a thing andlabel
is another. Caption is usually within the useful area of the chart. Labels are outside. LabelEixo X
is below the "X" axis and labelEixo Y
is on the side of the "Y" axis. Hug!– Solkarped