Labels

Showing posts with label MATPLOTLIB. Show all posts
Showing posts with label MATPLOTLIB. Show all posts

Tuesday, May 20, 2025

Matplotlib add minor ticker

 

import matplotlib.pyplot as plt

from matplotlib.ticker import MultipleLocator, FormatStrFormatter

majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(5)


t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)

fig, ax = plt.subplots()
plt.plot(t, s)

ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(majorFormatter)

# for the minor ticks, use no labels; default NullFormatter
ax.xaxis.set_minor_locator(minorLocator)

plt.show()

Reference:
https://matplotlib.org/2.0.2/examples/pylab_examples/major_minor_demo1.html

Friday, October 14, 2022

using matplotlib in C++

g++  -I/usr/include/python3.5/  -I/home/user/.local/lib/python3.5
/site-packages/numpy/core/include /usr/lib/x86_64-linux-gnu/libpython3.5m.so 
test.cc
// complie in a chromebook linux

#include "matplotlibcpp.h"
#include <vector>

namespace plt = matplotlibcpp;

int main() {
  std::vector<double> y = {1, 3, 2, 4};
  plt::plot(y);
  plt::savefig("minimal.pdf");
  plt::show();
}

Reference: 
https://matplotlib-cpp.readthedocs.io/en/latest/examples.html 
http://hilite.me/

Saturday, December 19, 2020

Matplotlib notes

# adjust the tick font size 

ax.tick_params(axis='both', which='minor', labelsize=8)

#set tick to right side

ax.yaxis.tick_right()

# remove space between start and end of the axis
plt.margins(x=0)
ax.margins(x=0)
plt.rcParams['axes.xmargin'] = 0

# remove extra fig for subplot
fig.delaxes(axes[1][2])
axes[1,2].set_axis_off()
axes.flat[-1].set_visible(False)


# better minus symbol
matplotlib.rcParams['axes.unicode_minus'] = False

# fontsize
plt.rcParams['font.size'] = '16'


import matplotlib.pyplot as plt

SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12

plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title

Reference:



Matplotlib legend location

 


Reference:

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html

Friday, August 28, 2020

Interactive with matplotlib figure

The interaction with the figure is very important.
Some keyboard shortcuts are helpful.

e.g.
After zoom the figure, you can press 'r' or 'h' to recover to default size.
'f' can show the figure in fullscreen.




Reference:

Wednesday, July 10, 2019

Tricks on ploting figure with matplotlib

plot error-bar and upper limit

# lower & upper limits of the error
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)

ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
            xlolims=xlolims, xuplims=xuplims,
            uplims=uplims, lolims=lolims,
            marker='o', markersize=8,
            linestyle='none')



plt.legend(frameon=False)









Reference:
https://matplotlib.org/3.1.0/gallery/statistics/errorbar_limits.html

Wednesday, May 8, 2019

Solutions to problems when plot with matplotlib

1. can not find 'tkinter' in matplotlib
import matplotlib matplotlib.use('agg')

Plot contour/scatter figure by matplotlib

This is my code.

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np

x,y,z = np.loadtxt('GRID007.dat',unpack=True)
#plt.tricontourf(x,y,z,np.linspace(int(min(z)),120,30,endpoint=True))
#plt.colorbar(ticks=range(int(min(z)),130,10))
''' plt.tricontourf(x,y,z,[min(z),37,74,111])
'''
plt.scatter(x,y,c=z,vmax=120)
plt.plot(1.07204,0.762,color='red',linestyle="None",marker='*',label=r'best fit ($\chi^2=100$')
#plt.legend()
#plt.colorbar(ticks=[min(z),111,222,333])
plt.colorbar()
plt.savefig(filename='scatter007.pdf',format='pdf',dpi=300)
plt.show()