Skip to content

Instantly share code, notes, and snippets.

@HeinrichApfelmus
Last active October 11, 2018 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeinrichApfelmus/3e5caee296e3543d876e to your computer and use it in GitHub Desktop.
Save HeinrichApfelmus/3e5caee296e3543d876e to your computer and use it in GitHub Desktop.
Chart library examples — monadic vs pure API
plotSpectrum1 lattice n m = toFile def file $ do
layout_title .= "Spectrum of '" ++ name ++ "'"
layout_x_axis . laxis_title .= "momentum k (" ++ show m ++ " points)"
layout_y_axis . laxis_title .= "energy E"
setColors $ colorGradient blue (length rows)
forM_ rows $ \row -> plot (line "band" [row])
where
...
colorGradient color n =
[opaque $ blend (fromIntegral k/fromIntegral n) black color | k <- [1..n]]
plotSpectrum' n m = renderableToFile def file $ toRenderable chart
where
chart = tval energyBands
energyBands
= layout_title .~ "Spectrum - '" ++ name ++ "'"
$ layout_x_axis . laxis_title .~ "momentum k (" ++ show m ++ " points)"
$ layout_y_axis . laxis_title .~ "energy E (" ++ show n ++ " bands)"
$ layout_plots .~ map (toPlot . plotBand) (zip bands colors)
$ def
colors = colorGradient blue (length bands)
plotBand (band,c)
= plot_lines_values .~ [map (\(x,y,z) -> (x,y)) band]
$ plot_lines_style . line_color .~ c
$ def
...
colorGradient color n =
[opaque $ blend (fromIntegral k/fromIntegral n) black color | k <- [1..n]]
% test data (to keep example self-contained)
ks = 0:0.1:pi; m = length(ks);
bands = transpose([sin(ks); sin(2*ks); sin(3*ks)]); % m * 3 matrix
% plotting functionality
gradient = transpose( [ zeros(1,m); zeros(1,m); 1 - ks / pi ])
set(0, 'DefaultAxesColorOrder', gradient)
plot(bands)
xlabel(sprintf('momentum k (%d points)',m))
ylabel('energy E')
title('Spectrum of Hamiltonian')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment