Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change x and y axes in matplotlib?

I am using matplotlib to draw neuralnet. I found a code that draws neural net, but it is oriented from top to bottom. I would like to change the orientation from left to right. So basically I would like to change x and y axes after I already plotted all the shapes. Is there an easy way to do this? I also found an answer that said that you can change parameter "orientation" to horizontal (code below) but I don't really understand where in my code should I copy that. would that give me the same result?

matplotlib.pyplot.hist(x, 
                   bins=10, 
                   range=None, 
                   normed=False, 
                   weights=None, 
                   cumulative=False, 
                   bottom=None, 
                   histtype=u'bar', 
                   align=u'mid', 
                   orientation=u'vertical', 
                   rwidth=None, 
                   log=False, 
                   color=None, 
                   label=None, 
                   stacked=False, 
                   hold=None, 
                   **kwargs)
like image 836
user2896492634 Avatar asked Aug 19 '16 22:08

user2896492634


People also ask

How do I switch axes in MatPlotLib?

To switch the axes of the plot, set x_data and y_data of the axis 1 curve to axis 2 curve. Adjust the padding between and around the subplots. To display the figure, use show() method.

How do I change the X-axis values in MatPlotLib?

The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. List of xticks locations. Passing an empty list will remove all the xticks.

How do I change the y-axis values in MatPlotLib?

To specify the value of axes, create a list of characters. Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively. Plot the line using x and y, color=red, using plot() method. Make x and y margin 0.

How do you change Yticks in MatPlotLib?

To create a list of ticks, we will use numpy. arange(start, stop, step) with start as the starting value for the ticks, stop as the non-inclusive ending value and step as the integer space between ticks. Below example illustrate the matplotlib. pyplot.


1 Answers

What you have in your code is an example of how to launch an histogram in matplotlib. Notice you are using the pyplot default interface (and not necessarily building your own figure).

As so this line:

orientation=u'vertical',

should be:

orientation=u'horizontal',

, if you want the bars to go from left to right. This however will not help you with the y axis. For you to invert the y axis you should use the command:

plt.gca().invert_yaxis()

The following example shows you how to build an histogram from random data (asymmetric to be easier to perceive modifications). The first plot is the normal histogram, the second I change the histogram orientation; in the last I invert the y axis.

import numpy as np
import matplotlib.pyplot as plt

data = np.random.exponential(1, 100)

# Showing the first plot.
plt.hist(data, bins=10)
plt.show()

# Cleaning the plot (useful if you want to draw new shapes without closing the figure
# but quite useless for this particular example. I put it here as an example).
plt.gcf().clear()

# Showing the plot with horizontal orientation
plt.hist(data, bins=10, orientation='horizontal')
plt.show()

# Cleaning the plot.
plt.gcf().clear()

# Showing the third plot with orizontal orientation and inverted y axis.
plt.hist(data, bins=10, orientation='horizontal')
plt.gca().invert_yaxis()
plt.show()

The result for plot 1 is (default histogram):

default histogram in matplotlib

The second (changed bar orientation):

default histogram in matplotlib with changed orientation

And finally the third (inverted y axis):

Histogram in matplotlib with horizontal bars and inverted y axis

like image 99
armatita Avatar answered Oct 03 '22 16:10

armatita