GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colores
  • COLORES
  • PALETAS DE COLOR
  • R CODER
  • R CHARTS
  • Contacto
  • English

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Tamaño de la figura en matplotlib

Busca un tutorial

INICIO MATPLOTLIB TAMANO FIGURA

Tamaño de la figura en matplotlib

Por defecto matplotlib crea figuras de 6.4 x 4.8 pulgadas. Este tutorial explica cómo cambiar el tamaño de una figura, cómo indicarlo en centímetros o en píxeles y cómo afecta la resolución (DPI) al tamaño final de la imagen.

Cambiar el tamaño de la figura con figsize

El argumento figsize establece el ancho y el alto de la figura en pulgadas. Está disponible en plt.subplots y en plt.figure.

Cambiar el tamaño de la figura en matplotlib con figsize

import matplotlib.pyplot as plt

months = ["E", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
rain = [62, 48, 41, 47, 44, 22, 9, 12, 32, 60, 74, 70]

fig, ax = plt.subplots(figsize = (8, 3))
ax.bar(months, rain, color = "steelblue")
# plt.show()

Figura cuadrada

Utiliza el mismo valor para el ancho y el alto para crear una figura cuadrada. Si además necesitas que el área del gráfico sea cuadrada independientemente de las etiquetas utiliza ax.set_box_aspect(1).

import matplotlib.pyplot as plt

months = ["E", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
rain = [62, 48, 41, 47, 44, 22, 9, 12, 32, 60, 74, 70]

fig, ax = plt.subplots(figsize = (4, 4), facecolor = "#EEF2F7")
ax.bar(months, rain, color = "steelblue")
ax.set_title("figsize = (4, 4)", fontsize = 16, family = "monospace")
# plt.show()

Figura cuadrada en matplotlib

Usando plt.figure

Si trabajas con la interfaz de pyplot puedes establecer el tamaño al crear la figura con plt.figure.

import matplotlib.pyplot as plt

plt.figure(figsize = (10, 5))
plt.plot([1, 2, 3], [1, 4, 9])
# plt.show()

Cambiar el tamaño de una figura existente

El método set_size_inches cambia el tamaño de una figura que ya se ha creado. Es útil cuando la figura la ha creado otra función, como un gráfico de seaborn o de pandas.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])

fig.set_size_inches(10, 4)
# plt.show()

Tamaño de la figura en centímetros o en píxeles

Matplotlib solo admite pulgadas, pero puedes convertir otras unidades fácilmente. Una pulgada equivale a 2.54 centímetros, así que divide el tamaño en centímetros entre 2.54.

import matplotlib.pyplot as plt

cm = 1 / 2.54

fig, ax = plt.subplots(figsize = (15 * cm, 10 * cm))
ax.plot([1, 2, 3], [1, 4, 9])
# plt.show()

El tamaño en píxeles es el tamaño en pulgadas multiplicado por los DPI (puntos por pulgada). Para crear una imagen de exactamente 800 x 400 píxeles con 100 DPI necesitas una figura de 8 x 4 pulgadas:

import matplotlib.pyplot as plt

px = 1 / plt.rcParams["figure.dpi"]

fig, ax = plt.subplots(figsize = (800 * px, 400 * px))
ax.plot([1, 2, 3], [1, 4, 9])
# plt.show()

Resolución (DPI)

El argumento dpi controla la resolución de la figura. Un valor de DPI mayor genera una imagen más grande y nítida con las mismas proporciones, y los textos y las líneas también crecen. El valor por defecto es 100.

import matplotlib.pyplot as plt

months = ["E", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
rain = [62, 48, 41, 47, 44, 22, 9, 12, 32, 60, 74, 70]

fig, ax = plt.subplots(figsize = (4, 4), dpi = 50)
ax.bar(months, rain, color = "steelblue")
# plt.show()

Cambiar la resolución de una figura de matplotlib con dpi

Para cambiar la resolución del archivo que guardas utiliza el argumento dpi de savefig, que se explica en el tutorial sobre cómo guardar gráficos en matplotlib.

Cambiar el tamaño por defecto

Para usar el mismo tamaño en todas las figuras de un script o de un notebook cambia los parámetros figure.figsize y figure.dpi con rcParams. Todas las figuras que crees después tendrán ese tamaño.

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (8, 5)
plt.rcParams["figure.dpi"] = 120

Cuando una figura tiene varios subplots, el tamaño se reparte entre todos ellos. Consulta el tutorial sobre subplots en matplotlib para aprender a ajustar el tamaño y el espaciado de cada subplot.

R CODER website
Data visualization

Data visualization

A practical introduction

Comprar en Amazon
Better Data Visualizations

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Comprar en Amazon

También te puede interesar

Fuentes en matplotlib

Fuentes en matplotlib

Tipos de línea en matplotlib

Tipos de línea en matplotlib

Color de fondo en matplotlib

Color de fondo en matplotlib

Animaciones en matplotlib

Animaciones en matplotlib

Líneas de referencia en matplotlib

Líneas de referencia en matplotlib

Textos y anotaciones en matplotlib

Textos y anotaciones en matplotlib

Barra de color en matplotlib

Barra de color en matplotlib

Marcadores en matplotlib

Marcadores en matplotlib

Títulos en matplotlib

Títulos en matplotlib

Estilos en matplotlib

Estilos en matplotlib

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.