Change size of Plot area

Asked

Viewed 1,329 times

0

I generated a Plot scatter, but I can’t change the size of the screen it appears on my laptop. Can anyone help me with this?

plt.scatter(X,Y,label='Y(X)'); # sacater plot
plt.xlabel('X');
plt.ylabel('Y');
plt.legend();

I tried using the parameter plt.scatter(X,Y,label='Y(X)',figsize=(x,y) but it didn’t work

2 answers

0


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.

  • 1

    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, If the answer solved your problem, you can aceitá-la, by clicking on V 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.

  • @Lpcoutinho, to change the size of the caption just change the value of fontsize on the line plt.legend(fontsize=10). You can change it to 20, 30, 40, or 5, 8, etc.

  • @Lpcoutinho, if you want to change the size of fontsize of the label plt.xlabel('eixo X', fontsize=15) and plt.ylabel('eixo y', fontsize=15), just also change the numerical values to whatever you want.

  • 1

    @Solkarped, in label=f'{legenda}' can be done directly label=legenda the same thing in the marker=f'{fp}'. There is no need for this formatting. Hug!

  • @Lpcoutinho, legenda is a thing and label is another. Caption is usually within the useful area of the chart. Labels are outside. Label Eixo X is below the "X" axis and label Eixo Y is on the side of the "Y" axis. Hug!

Show 1 more comment

0

import numpy as np
import matplotlib.pyplot as plt

Creating dummy data:

X = np.arange(1,20)
Y = X * np.random.randint(19, 40, 19)

Here is the line that does the job of changing the size of the Plot:

plt.figure(figsize=(20,10))

Remainder of your code:

plt.scatter(X,Y,label='Y(X)')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()

Show the Plot:

plt.show()

Edit

To add the font size just use fontsize inside xlabel, ylabel and legend

plt.xlabel('X', fontsize = 18)
plt.ylabel('Y', fontsize = 18)
plt.legend(fontsize = 20)
  • 1

    Great, you solved the problem fast!!! 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();

  • We are there, my dear! Hug!

  • I added the answer to your new question. Hug!

  • Very good! You are Beast, but I want to change the font of the legend that is inside the Plot, in: plt.scatter(X,Y,label='Y(X)');

  • The idea is the same: plt.legend(fontsize = 20)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.