MCPcopy Create free account
hub / github.com/Derive-Risk/pyfair / generate_image

Method generate_image

pyfair/report/distribution.py:82–133  ·  view source on GitHub ↗

Provides histogram(s) with PDF curve(s) Returns ------- (matplotlib.figure, matplotlib.ax) A tuple containing the figure and axis generated Examples -------- >>> m = pyfair.model.FairModel.from_json('model_1.json') >>> dc = pyfair

(self)

Source from the content-addressed store, hash-verified

80 return (fig, ax)
81
82 def generate_image(self):
83 """Provides histogram(s) with PDF curve(s)
84
85 Returns
86 -------
87 (matplotlib.figure, matplotlib.ax)
88 A tuple containing the figure and axis generated
89
90 Examples
91 --------
92 >>> m = pyfair.model.FairModel.from_json('model_1.json')
93 >>> dc = pyfair.report.FairDistributionCurve(m)
94 >>> fig, ax = dc.generate_image()
95
96 """
97 # Setup plots
98 fig, ax = plt.subplots(figsize=(16, 6))
99 plt.subplots_adjust(bottom=.2)
100 ax.axes.set_title('Risk Distribution', fontsize=20)
101 # Format X axis
102 ax.axes.xaxis.set_major_formatter(StrMethodFormatter('${x:,.0f}'))
103 ax.axes.xaxis.set_tick_params(rotation=-45)
104 ax.set_ylabel('Frequency Histogram')
105 for tick in ax.axes.xaxis.get_major_ticks():
106 tick.label.set_horizontalalignment('left')
107 # Draw histrogram for each model
108 legend_labels = []
109 for name, model in self._input.items():
110 legend_labels.append(name)
111 plt.hist(
112 [model.export_results()['Risk']],
113 bins=25,
114 alpha=.3
115 )
116 ax.legend(legend_labels, frameon=False)
117 # Min and Max post graphing
118 xmin, xmax = ax.get_xlim()
119 # Now draw twin axis a d style
120 tyax = plt.twinx(ax)
121 tyax.set_ylabel('PDF')
122 tyax.set_yticks([])
123 # Plot for each
124 for name, model in self._input.items():
125 risk = model.export_results()['Risk']
126 # Catch warnings as we're "fitting" with known shape parameters.
127 with warnings.catch_warnings():
128 warnings.simplefilter("ignore")
129 beta_curve = beta(*beta.fit(risk))
130 space = np.linspace(0, xmax, 1000)
131 tyax.plot(space, beta_curve.pdf(space))
132 plt.margins(0)
133 return (fig, ax)

Callers 1

_get_distributionMethod · 0.95

Calls 1

export_resultsMethod · 0.45

Tested by

no test coverage detected