Skip to content

Commit 7614e8d

Browse files
committed
add hist plot method
1 parent 6904d55 commit 7614e8d

1 file changed

Lines changed: 78 additions & 1 deletion

File tree

danfojs/src/plotting/plot.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ export class Plot {
486486
// params.forEach(param => { //TODO accept individual configuration for traces
487487
// trace[param] = config[param]
488488
// })
489-
trace['x'] = this.ndframe.index
489+
trace['y'] = this.ndframe.index
490490
trace["x"] = this.ndframe[c_name].values
491491
trace['name'] = c_name
492492
trace['type'] = "scatter"
@@ -504,6 +504,83 @@ export class Plot {
504504
}
505505

506506

507+
/**
508+
* Plot columns in a Series/DataFrame as Histograms.
509+
* Uses the Plotly as backend, so supoorts Plotly's configuration parameters
510+
* @param {string} div Name of the div to show the plot
511+
* @param {Object} config configuration options for making Plots, supports Plotly parameters
512+
*/
513+
hist(config = {}) {
514+
515+
let ret_params = this.__get_plot_params(config)
516+
let this_config = ret_params[0]
517+
let params = ret_params[1]
518+
519+
if (this.ndframe instanceof Series) {
520+
let trace = {}
521+
522+
params.forEach(param => {
523+
if (!param == "layout") {
524+
trace[param] = config[param]
525+
}
526+
})
527+
528+
trace["x"] = this.ndframe.values
529+
trace['type'] = "histogram"
530+
531+
newPlot(this.div, [trace], this_config['layout']);
532+
533+
} else if (utils.__key_in_object(this_config, 'x')) {
534+
//plot as vertical histogram
535+
let trace = {}
536+
params.forEach(param => {
537+
if (!param == "layout") {
538+
trace[param] = config[param]
539+
}
540+
})
541+
542+
trace['x'] = this.ndframe[this_config['y']].values
543+
trace['type'] = "histogram"
544+
545+
newPlot(this.div, [trace], this_config['layout']);
546+
547+
} else if (utils.__key_in_object(this_config, 'y')) {
548+
//plot as vertical histogram
549+
let trace = {}
550+
params.forEach(param => {
551+
if (!param == "layout") {
552+
trace[param] = config[param]
553+
}
554+
})
555+
556+
trace['y'] = this.ndframe[this_config['y']].values
557+
trace['type'] = "histogram"
558+
559+
newPlot(this.div, [trace], this_config['layout']);
560+
561+
} else {
562+
let data = []
563+
let cols_to_plot;
564+
565+
if (utils.__key_in_object(this_config, "columns")) {
566+
cols_to_plot = this.____check_if_cols_exist(this_config['columns'])
567+
} else {
568+
cols_to_plot = this.ndframe.column_names
569+
}
570+
571+
cols_to_plot.forEach(c_name => {
572+
let trace = {}
573+
trace["x"] = this.ndframe[c_name].values
574+
trace['name'] = c_name
575+
trace['type'] = "histogram"
576+
data.push(trace)
577+
578+
})
579+
newPlot(this.div, data, this_config['layout']);
580+
581+
}
582+
583+
}
507584

508585

509586
__get_plot_params(config) {

0 commit comments

Comments
 (0)