Page tree
Skip to end of metadata
Go to start of metadata


В питоне имеется огромное количество различных библиотек для рисования графиков, но далеко не все из них (на самом деле практически ни одна) не годится для создания графиков, которые потом можно вставить в публикацию.

Для того, чтобы считать библиотеку годной для создания графиков для публикаций, необходимо выполнения следующих требований:

  • экспорт в векторный формат (SVG/PDF).
  • Достаточно тонкая настройка всяких элементов оформления вроде осей и подписей.

Рассмотрим разные библиотеки.


Pyplot

Плюсы:

  • Умеет делать SVG при помощи `plt.savefig("test.svg", format="svg")`

  • Имеет огромное количество настроек нужных и ненужных

Минусы:

  • Удручающее отсутствие интерактивности (для публикаций это и не надо, но если хочется чего-то большего...)
  • API прибыл прямиком из каменного века
  • Иногда работает, а иногда нет

TODO добавить примеры


Plotly

Плюсы:

  • Интерактивный веб-редактор!

  • Очень приятный современный API, который позволяет настроить все, что нужно
  • Выглядит красиво
  • Интерактивно прямо сразу
  • Начиная с версии 3.2.0 есть векторный экспорт в бесплатной версии : https://plot.ly/python/static-image-export/

Минусы:

  • Экспорт в svg только в платной версии 
Plotly example
import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
import plotly.io as pio

init_notebook_mode(connected=True)


fig = go.Figure( data = [hist12_plot, hist14_plot, hist16_plot, hist18_plot], layout= go.Layout(barmode='overlay'))


iplot(fig)


pio.write_image(fig, 'images/fig1.svg')



Beakerx

Плюсы:

  • Сравнительно человеческий API

  • Интерактивность из коробки
  • Есть штатный экспорт в SVG
  • Работает на разных языках программирования

Минусы:

  • Очень мало настроек для оформления. Прямо как есть в публикацию не сунешь.
  • Сыроват
from beakerx import *


plot = Plot(xLabel = "Delay (\u03bcs)", yLabel = "Reconstructed count rate")
plot.setXBound(7,50)
plot.setYBound(25.9e3,26.4e3)
plot.add(Area(
    x = irregular_simple['x'], 
    y= (irregular_simple['y'] + irregular_simple['y.err']).tolist(), 
    base = (irregular_simple['y'] - irregular_simple['y.err']).tolist(),
    color = "#22FF0000"
))
plot.add(Line(
    x = irregular_simple['x'], 
    y= irregular_simple['y'],
    displayName = "simple",
    color = "#FFFF0000",
))
plot.add(Area(
    x = irregular_chunked['x'], 
    y= (irregular_chunked['y'] + irregular_chunked['y.err']).tolist(), 
    base = (irregular_chunked['y'] - irregular_chunked['y.err']).tolist(),
    color = "#22008c19"
))
plot.add(Line(
    x = irregular_chunked['x'], 
    y= irregular_chunked['y'],
    displayName = "chunked",
    color = "#FF008c19",
    style = "DASHDOT"
))
plot.add(Area(
    x = irregular_mean['x'], 
    y= (irregular_mean['y'] + irregular_mean['y.err']).tolist(), 
    base = (irregular_mean['y'] - irregular_mean['y.err']).tolist(),
    color = "#220000FF"
))
plot.add(Line(
    x = irregular_mean['x'], 
    y= irregular_mean['y'],
    displayName = "arithmetic mean",
    color = "#FF0000FF",
    style = "DOT"
))
plot

TODO добавить картинку


Bokeh

Все то же самое, что и в Pyplot, но уже не каменный век, а наверное железный

from bokeh.plotting import figure, show, output_file
from bokeh.models import Band, ColumnDataSource
from bokeh.io import output_notebook, export_svgs
output_notebook()


fig = figure(output_backend="svg", x_range = (7,50), y_range = (25.9e3,26.4e3),plot_width = 800, plot_height = 400)
fig.xaxis.axis_label = 'Delay (\u03bcs)'
fig.xaxis.axis_label_text_font_size = "12pt"
fig.yaxis.axis_label = 'Reconstructed count rate'
fig.yaxis.axis_label_text_font_size = "12pt"

fig.line(
    x = irregular_simple['x'], 
    y = irregular_simple['y'],
    legend = "simple",
    line_color = "red",
    line_width = 4
)

irregular_simple['lower'] =  irregular_simple['y'] - irregular_simple['y.err']
irregular_simple['upper'] =  irregular_simple['y'] + irregular_simple['y.err']


simple_area = Band(
    base = 'x',
    upper = 'upper',
    lower = 'lower',
    source = ColumnDataSource(irregular_simple),
    fill_alpha=0.1,
    fill_color = 'red'
)

fig.add_layout(simple_area)

fig.line(
    x = irregular_chunked['x'], 
    y = irregular_chunked['y'],
    legend = "chunked",
    line_color = "green",
    line_dash = "dashdot",
    line_width = 4
)

irregular_chunked['lower'] =  irregular_chunked['y'] - irregular_chunked['y.err']
irregular_chunked['upper'] =  irregular_chunked['y'] + irregular_chunked['y.err']


chunked_area = Band(
    base = 'x',
    upper = 'upper',
    lower = 'lower',
    source = ColumnDataSource(irregular_chunked),
    fill_alpha=0.1,
    fill_color = 'green'
)

fig.add_layout(chunked_area)

fig.line(
    x = irregular_mean['x'], 
    y= irregular_mean['y'],
    legend = "arithmetic mean",
    line_color = "blue",
    line_dash = "dotted",
    line_width = 4
)

irregular_mean['lower'] =  irregular_mean['y'] - irregular_mean['y.err']
irregular_mean['upper'] =  irregular_mean['y'] + irregular_mean['y.err']


mean_area = Band(
    base = 'x',
    upper = 'upper',
    lower = 'lower',
    source = ColumnDataSource(irregular_mean),
    fill_alpha = 0.1,
    fill_color = 'blue'
)

fig.add_layout(mean_area)

fig.legend.location = "bottom_left"

show(fig)

TODO добавить картинку

Экспорт:

export_svgs(fig, filename="images/irregular.svg")

Преобразование SVG в PDF (все библиотеки): 

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF

drawing = svg2rlg("images/irregular.svg")
renderPDF.drawToFile(drawing, "images/irregular.pdf")
  • No labels