Difference between revisions of "Matplotlib"
Jump to navigation
Jump to search
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Python]] | [[Category:Python]] | ||
Using the [https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py pyplot] functions only until now. | Using the [https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py pyplot] functions only until now. | ||
+ | |||
;import matplotlib.pyplot as plt | ;import matplotlib.pyplot as plt | ||
:Get the pyplot library. We assume this is done in all examples on this page. | :Get the pyplot library. We assume this is done in all examples on this page. | ||
Line 18: | Line 19: | ||
;plt.legend() | ;plt.legend() | ||
:Add a legend to the plot. Default legend is the label, specify an alternative legend text as list. | :Add a legend to the plot. Default legend is the label, specify an alternative legend text as list. | ||
+ | ;plt.ylim(min,max) | ||
+ | :Fix the y-axis (xlim for the x-axis) | ||
;plt.show() | ;plt.show() | ||
:Show the current figure. | :Show the current figure. | ||
Line 31: | Line 34: | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
plt.plot(array) | plt.plot(array) | ||
− | plt.suptitle('Set the graph | + | plt.title('Set the graph title') |
+ | plt.suptitle('Set the graph super title') | ||
plt.xlabel('Set the x label') | plt.xlabel('Set the x label') | ||
plt.ylabel('Set the y label') | plt.ylabel('Set the y label') | ||
Line 45: | Line 49: | ||
==Formatting== | ==Formatting== | ||
− | ;plt.plot | + | ;plt.plot(x,y,format) |
:Draw lines | :Draw lines | ||
− | ;plt.bar | + | ;plt.bar(x,y,format) |
:Draw bars | :Draw bars | ||
− | ;plt.scatter | + | ;plt.scatter(x,y,format) |
:Draw points | :Draw points | ||
− | + | The 'x' and 'format' arguments are optional. | |
+ | 'x' and 'y' are lists that specify respectively the X-axis and the Y-axis. When only a labeled list like numpy arrays or pandas Series is provided the index makes the X, the values the Y. | ||
− | + | The next argument defines the format as <colorchar><format> [https://matplotlib.org/stable/gallery/lines_bars_and_markers/marker_reference.html]. | |
{| class="wikitable" | {| class="wikitable" | ||
|+ Formatting strings | |+ Formatting strings | ||
Line 83: | Line 88: | ||
| Yellow Balls | | Yellow Balls | ||
|} | |} | ||
+ | |||
+ | =Runtime errors= | ||
+ | If you see errors like this in your uwsgi logfile (can be in deamon.log too) | ||
+ | uwsgi: Matplotlib created a temporary config/cache directory at /tmp/matplotlib-nmvqnrki because the default path (/var/www/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing. | ||
+ | You can fix this by setting the MPLCONFIGDIR environment variable before you import matplotlib | ||
+ | os.environ['MPLCONFIGDIR'] = '/var/tmp' | ||
+ | import matplotlib |
Latest revision as of 13:49, 7 November 2021
Using the pyplot functions only until now.
- import matplotlib.pyplot as plt
- Get the pyplot library. We assume this is done in all examples on this page.
- plt.figure(2)
- Initialize a new figure. figure(1) is initialized by default.
- plt.subplot(211)
- Create 1st subplot in figure(1) (212 is the 2nd subplot in figure(1). Return an 'AxesSubplot' object.
- 211 : Split the figure in 2, figure(1) subplot 1
- The subplot can be specified as 2,1,1 too which comes in handy when you have to generate subplots on the fly.
- fig,ax = plt.subplots()
- Return Figure and AxeSubPlot object. Same as:
fig = plt.figure()
ax = fig.add_subplot(111)
- plt.plot(array)
- Create the graphical object from the values in the array. Show the graph if your environment (e.g. jupyter) supports it.
- When you do more plots without changing figure or subplot the result will be added to the current figure.
- plt.legend()
- Add a legend to the plot. Default legend is the label, specify an alternative legend text as list.
- plt.ylim(min,max)
- Fix the y-axis (xlim for the x-axis)
- plt.show()
- Show the current figure.
- plt.savefig('plot.png')
- Save the plot on file
Show the image:
firefox plot.png
eog plot.png
shotwell plot.png
Selfexplaning example code:
plt.plot(array)
plt.title('Set the graph title')
plt.suptitle('Set the graph super title')
plt.xlabel('Set the x label')
plt.ylabel('Set the y label')
plt.show()
With subplots, more lines and legends
ax = plt.subplot(111)
lineA = ax.plot(array,label='Alabel')
lineB = ax.plot(array,label='Blabel')
ax.legend()
Formatting
- plt.plot(x,y,format)
- Draw lines
- plt.bar(x,y,format)
- Draw bars
- plt.scatter(x,y,format)
- Draw points
The 'x' and 'format' arguments are optional. 'x' and 'y' are lists that specify respectively the X-axis and the Y-axis. When only a labeled list like numpy arrays or pandas Series is provided the index makes the X, the values the Y.
The next argument defines the format as <colorchar><format> [1].
Format | Meaning |
---|---|
b- | Blue Solid line, the default |
r-- | Red Dashed line |
b: | Blue Dotted line |
r. | Red Dots |
g-. | Green dashes and dots |
gb | Green Squares |
y^ | Yellow Triangles |
yo | Yellow Balls |
Runtime errors
If you see errors like this in your uwsgi logfile (can be in deamon.log too)
uwsgi: Matplotlib created a temporary config/cache directory at /tmp/matplotlib-nmvqnrki because the default path (/var/www/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
You can fix this by setting the MPLCONFIGDIR environment variable before you import matplotlib
os.environ['MPLCONFIGDIR'] = '/var/tmp' import matplotlib