Last active
October 11, 2018 14:05
-
-
Save HeinrichApfelmus/3e5caee296e3543d876e to your computer and use it in GitHub Desktop.
Chart library examples — monadic vs pure API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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