Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In matplotlib, how do you display an axis on both sides of the figure?

Tags:

I want to draw a plot with matplotlib with axis on both sides of the plot, similar to this plot (the color is irrelevant to this question):

plot

How can I do this with matplotlib?

Note: contrary to what is shown in the example graph, I want the two axis to be exactly the same, and want to show only one graph. Adding the two axis is only to make reading the graph easier.

like image 465
static_rtti Avatar asked Aug 09 '11 15:08

static_rtti


People also ask

What is dual axis plot MatPlotLib?

MatPlotLib with Python Moreso, when plotting curves with different units together. Matplotlib supports this with the twinxand twiny functions. In the following example, the plot has dual y axes, one showing exp(x) and the other showing log(x) − import matplotlib. pyplot as plt import numpy as np fig = plt.

How do I put the Y-axis on the right side in Python?

MatPlotLib with Python To shift the Y-axis ticks from left to right, use ax. yaxis. tick_right() where ax is axis created using add_subplot(xyz) method.


1 Answers

You can use tick_params() (this I did in Jupyter notebook):

import matplotlib.pyplot as plt  bar(range(10), range(10)) tick_params(labeltop=True, labelright=True) 

Generates this image:

Bar plot with both x and y axis labeled the same

UPD: added a simple example for subplots. You should use tick_params() with axis object.

This code sets to display only top labels for the top subplot and bottom labels for the bottom subplot (with corresponding ticks):

import matplotlib.pyplot as plt  f, axarr = plt.subplots(2)  axarr[0].bar(range(10), range(10)) axarr[0].tick_params(labelbottom=False, labeltop=True, labelleft=False, labelright=False,                      bottom=False, top=True, left=False, right=False)  axarr[1].bar(range(10), range(10, 0, -1)) axarr[1].tick_params(labelbottom=True, labeltop=False, labelleft=False, labelright=False,                      bottom=True, top=False, left=False, right=False) 

Looks like this:

Subplots ticks config example

like image 127
kishkin Avatar answered Oct 04 '22 18:10

kishkin