options to control the legend and font

This commit is contained in:
2026-02-27 18:33:19 +01:00
parent 2f7924e580
commit 3823372449

View File

@@ -9,6 +9,7 @@ import logging
import argparse import argparse
from pathlib import PurePath##https://docs.python.org/3/library/pathlib.html#module-pathlib from pathlib import PurePath##https://docs.python.org/3/library/pathlib.html#module-pathlib
import numpy as np import numpy as np
import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from scipy.stats import norm from scipy.stats import norm
import pandas as pd import pandas as pd
@@ -44,8 +45,16 @@ parser.add_argument('--normalizey', action="store_true",
help='Set y-axis to normalized probability density') help='Set y-axis to normalized probability density')
parser.add_argument('--log', default="INFO", parser.add_argument('--log', default="INFO",
help='Console log level: Number or DEBUG, INFO, WARNING, ERROR') help='Console log level: Number or DEBUG, INFO, WARNING, ERROR')
parser.add_argument('--legend', action="store_true",
help='Put legend on the PDF graph')
parser.add_argument('--graphinfo', action="store_true", parser.add_argument('--graphinfo', action="store_true",
help='Put information on the PDF graph') help='Put information on the PDF graph')
parser.add_argument('--xlabel',
help='X-axis label, if needed')
parser.add_argument('--outfile',
help="output graph to PDF file")
parser.add_argument('--fontsize', default=14, type=int,
help="Adjust font size")
args = parser.parse_args() args = parser.parse_args()
@@ -101,9 +110,10 @@ prob = norm.cdf(upperbound, mean, stddev) - norm.cdf(lowerbound, mean, stddev)
#print("probability: %f", prob) #print("probability: %f", prob)
info = -np.emath.log2(prob) info = -np.emath.log2(prob)
#print("information content: %f bits", info) #print("information content: %f bits", info)
## set default fontsize
matplotlib.rcParams['font.size']=args.fontsize
## place text on plot: https://matplotlib.org/3.3.4/gallery/recipes/placing_text_boxes.html ## place text on plot: https://matplotlib.org/3.3.4/gallery/recipes/placing_text_boxes.html
fig, ax = plt.subplots() fig, ax = plt.subplots()
if args.graphinfo:#put info on corner of graph if args.graphinfo:#put info on corner of graph
textstr = '\n'.join(( textstr = '\n'.join((
r'$n=%d$' % (samplesize), r'$n=%d$' % (samplesize),
@@ -113,9 +123,9 @@ if args.graphinfo:#put info on corner of graph
r'$I=%.2f$ bits' % (info, ))) r'$I=%.2f$ bits' % (info, )))
# these are matplotlib.patch.Patch properties # these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
# place a text box in upper left in axes coords # place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14, ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=args.fontsize,
verticalalignment='top', bbox=props) verticalalignment='top', bbox=props)
x = np.linspace(mean-3*stddev, mean+3*stddev, 500) x = np.linspace(mean-3*stddev, mean+3*stddev, 500)
@@ -130,11 +140,16 @@ plt.plot(x, y, 'b-', label='Normal distribution')
#yt = scipy.stats.t.pdf(x, len(data)-1, mean, stddev) #yt = scipy.stats.t.pdf(x, len(data)-1, mean, stddev)
#plt.plot(x, yt, 'g-', label='T Distribution') #plt.plot(x, yt, 'g-', label='T Distribution')
coloredregion = (x >= lowerbound) & ( x <= upperbound ) #select x values coloredregion = (x >= lowerbound) & ( x <= upperbound ) #select x values
plt.fill_between(x, 0, y, where=coloredregion, color="grey", alpha=0.5, label="Design range") plt.fill_between(x, 0, y, where=coloredregion, color="grey", alpha=0.5, label="Design range",)
plt.xlabel('X') if args.xlabel:
plt.xlabel(args.xlabel)
plt.ylabel('Probability density') plt.ylabel('Probability density')
plt.legend() if args.legend:
plt.grid(True) plt.legend()
#plt.grid(True)
top = plt.ylim()[1] top = plt.ylim()[1]
plt.show()
if args.outfile:
plt.savefig(args.outfile,bbox_inches='tight')
else:
plt.show()