- Scala for Data Science
- Pascal Bugnion
- 382字
- 2021-07-23 14:33:05
Customizing plots
We now have a curve on our chart. Let's add a few more:
scala> val f2x = sigmoid(2.0*x) f2x: breeze.linalg.DenseVector[Double] = DenseVector(3.353501304664E-4... scala> val f10x = sigmoid(10.0*x) f10x: breeze.linalg.DenseVector[Double] = DenseVector(4.24835425529E-18... scala> plt += plot(x, f2x, name="S(2x)") breeze.plot.Plot = breeze.plot.Plot@63d6a0f8 scala> plt += plot(x, f10x, name="S(10x)") breeze.plot.Plot = breeze.plot.Plot@63d6a0f8 scala> fig.refresh()
Looking at the figure now, you should see all three curves in different colors. Notice that we named the data series as we added them to the plot, using the name=""
keyword argument. To view the names, we must set the legend
attribute:
scala> plt.legend = true

Our plot still leaves a lot to be desired. Let's start by restricting the range of the x axis to remove the bands of white space on either side of the plot:
scala> plt.xlim = (-4.0, 4.0) plt.xlim: (Double, Double) = (-4.0,4.0)
Now, notice how, while the x ticks are sensibly spaced, there are only two y ticks: at 0 and 1. It would be useful to have ticks every 0.1 increment. Breeze does not provide a way to set this directly. Instead, it exposes the underlying JFreeChart Axis object belonging to the current plot:
scala> plt.yaxis org.jfree.chart.axis.NumberAxis = org.jfree.chart.axis.NumberAxis@0
The Axis
object supports a .setTickUnit
method that lets us set the tick spacing:
scala> import org.jfree.chart.axis.NumberTickUnit import org.jfree.chart.axis.NumberTickUnit scala> plt.yaxis.setTickUnit(new NumberTickUnit(0.1))
JFreeChart allows extensive customization of the Axis
object. For a full list of methods available, consult the JFreeChart documentation (http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/axis/Axis.html).
Let's also add a vertical line at x=0 and a horizontal line at f(x)=1. We will need to access the underlying JFreeChart plot to add these lines. This is available (somewhat confusingly) as the .plot
attribute in our Breeze Plot
object:
scala> plt.plot org.jfree.chart.plot.XYPlot = org.jfree.chart.plot.XYPlot@17e4db6c
We can use the .addDomainMarker
and .addRangeMarker
methods to add vertical and horizontal lines to JFreeChart XYPlot
objects:
scala> import org.jfree.chart.plot.ValueMarker import org.jfree.chart.plot.ValueMarker scala> plt.plot.addDomainMarker(new ValueMarker(0.0)) scala> plt.plot.addRangeMarker(new ValueMarker(1.0))
Let's also add labels to the axes:
scala> plt.xlabel = "x" plt.xlabel: String = x scala> plt.ylabel = "f(x)" plt.ylabel: String = f(x)
If you have run all these commands, you should have a graph that looks like this:

We now know how to customize the basic building blocks of a graph. The next step is to learn how to change how curves are drawn.
- Clojure Programming Cookbook
- C# 7 and .NET Core Cookbook
- MySQL數(shù)據(jù)庫(kù)管理實(shí)戰(zhàn)
- ClickHouse性能之巔:從架構(gòu)設(shè)計(jì)解讀性能之謎
- Docker技術(shù)入門(mén)與實(shí)戰(zhàn)(第3版)
- 摩登創(chuàng)客:與智能手機(jī)和平板電腦共舞
- OpenCV實(shí)例精解
- 程序員考試案例梳理、真題透解與強(qiáng)化訓(xùn)練
- Java面向?qū)ο蟪绦蜷_(kāi)發(fā)及實(shí)戰(zhàn)
- ADI DSP應(yīng)用技術(shù)集錦
- Responsive Web Design by Example
- R大數(shù)據(jù)分析實(shí)用指南
- RISC-V體系結(jié)構(gòu)編程與實(shí)踐(第2版)
- 小型編譯器設(shè)計(jì)實(shí)踐
- JavaScript程序設(shè)計(jì):基礎(chǔ)·PHP·XML