nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.
MIT License
2.41k stars 215 forks source link

No colorbar displayed with contour(f) plot #255

Open Sunsheep opened 5 years ago

Sunsheep commented 5 years ago

I created a plot with contour(f) and noticed that no colorbar is created while saving to tex/pgfplots. However, the colorbar gets plotted in the pdf output.

Here is a minimal example of the used python code:

"""
Demo of the `streamplot` function.

A streamplot, or streamline plot, is used to display 2D vector fields. This
example shows a few features of the stream plot function:

    * Varying the color along a streamline.
    * Varying the density of streamlines.
    * Varying the line width along a stream line.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

fig0, ax = plt.subplots()
ax.set(aspect='equal')
plt.rc('text', usetex=True)
strm = ax.streamplot(X, Y, U, V, color="k")
#ax0.contour(X, Y, speed)
cntr1 = ax.contourf(X, Y, speed, levels=14, cmap="YlGnBu_r")
cbar = fig0.colorbar(cntr1)
cbar.ax.set_ylabel('verbosity coefficient')
fig0.colorbar(cntr1, ax=ax)
print(cntr1.levels)
#fig0.colorbar(strm.lines)

# Produce output and write to file
from matplotlib2tikz import save as tikz_save
tikz_save("output/" + "filename" + ".tikz",figureheight='8cm', figurewidth='\\textwidth',               strict=False,show_info=False)
plt.savefig("output/" + "filename" + ".pdf")
plt.show()

And here is the pgfplots output :

% This file was created by matplotlib2tikz v0.6.18.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.0670511341791619,0.142022299115725,0.418915801614764}
\definecolor{color1}{rgb}{0.145051903114187,0.204567474048443,0.580699730872741}
\definecolor{color2}{rgb}{0.137301038062284,0.313079584775086,0.632372164552095}
\definecolor{color3}{rgb}{0.127028066128412,0.432941176470588,0.689088811995386}
\definecolor{color4}{rgb}{0.115386389850058,0.570334486735871,0.753125720876586}
\definecolor{color5}{rgb}{0.208396770472895,0.665928489042676,0.763460207612457}
\definecolor{color6}{rgb}{0.334994232987313,0.743437139561707,0.757001153402537}
\definecolor{color7}{rgb}{0.503575547866205,0.806074586697424,0.73279507881584}
\definecolor{color8}{rgb}{0.68959630911188,0.87841599384852,0.714709727028066}
\definecolor{color9}{rgb}{0.830649750096117,0.93356401384083,0.701914648212226}
\definecolor{color10}{rgb}{0.931349480968858,0.973302575932334,0.69842368319877}
\definecolor{color11}{rgb}{0.977854671280277,0.991387927720108,0.801768550557478}

\begin{axis}[
height=8cm,
tick align=outside,
tick pos=left,
width=\textwidth,
x grid style={white!69.01960784313725!black},
xmin=-3, xmax=3,
y grid style={white!69.01960784313725!black},
ymin=-3, ymax=3
]
\addplot [draw=none, fill=color0, colormap/viridis]
table [row sep=\\]{%
x                      y\\ 
-2.727272727272725e-01 -2.773318691135177e-01\\ 
-2.121212121212119e-01 -2.751984619803281e-01\\ 
(...) 

*Notice: I only attached the pgfplots header, because the file is tho big. But you can see, there is no colormap/colorbar command while defining the axis.

*Oh, and by the way: the matplotlib axis option aspect='equal' isnt exported to. That might be another issue.

Edit

Giving the streamline plot a non static color returns a other object as the contour plots - the colorbar gets exported correctly when using streamlines colors option. *see matplotlib documentation

Edit 2

Why matplot2tikz doesn't use `\addplot3´ and ´surf´ instead of filling curves?

FelixBiermann commented 1 year ago

I have the same issue. Is this bug going to be fixed?

Thank you for your great work anyway!