Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I am trying to plot two separate quantities on the same graph using twiny as follows:

fig = figure()

ax = fig.add_subplot(111)

ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')

ax.set_yscale('log')

ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))

ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))

ax.set_xlabel('Rotational period (hrs)')

ax.set_ylabel('Orbital radius (km), logarithmic')

ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')

ax2 = ax.twiny()

ax2.plot(v,r,'k-')

ax2.set_xlabel('Linear speed (ms-1)')

show()

and the data is presented fine, but I am having the problem that the figure title is overlapping with the axes labels on the secondary x-axis so that it's barely legible (I wanted to post a picture example here, but I don't have a high enough rep yet).

I'd like to know if there's a straightforward way to just shift the title directly up a few tens of pixels so that the chart looks prettier.

1 Answer

0 votes
by (16.8k points)
edited by

scala> case class PRange(start: Int, end: Int, step: Int = 1)

defined class PRange

scala> implicit def intWithTildyArrow(i: Int) = new {

     |   def ~>(j: Int) = PRange(i, j)

     | }

intWithTildyArrow: (i: Int)java.lang.Object{def ~>(j: Int): PRange}

scala> implicit def prangeWithTildyArrow(p: PRange) = new {

     |   def ~>(step: Int) = p.copy(step = step)

     | }

prangeWithTildyArrow: (p: PRange)java.lang.Object{def ~>(step: Int): PRange}

scala> implicit def pSlice[A](coll: List[A]) = new {

     |   def apply(prange: PRange) = {

     |     import prange._

     |     coll.slice(start, end).grouped(step).toList.map(_.head)

     |   }

     | }

pSlice: [A](coll: List[A])java.lang.Object{def apply(prange: PRange): List[A]}

scala> val xs = List.range(1, 10)

xs: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> xs(3 ~> 9)

res32: List[Int] = List(4, 5, 6, 7, 8, 9)

scala> xs(3 ~> 9 ~> 2)

res33: List[Int] = List(4, 6, 8)

Enroll in the Scala course in Toronto to get professionally certified. 

Related questions

+2 votes
2 answers
asked May 23, 2019 in Python by Ritik (3.5k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Karan Singh (4.1k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...