Barplot with log y-axis program syntax using mplotlib pyplot

I understand that this question was asked earlier ( Python Pyplot Bar patches disappear when using the log scale ), but the answer did not help me. I set my pyplot.bar (x_values, y_values ​​etc., Log = True), but received an error message:

"TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" 

I searched in vain for the actual pyplot code example that uses the bar graph with the y axis set to register, but could not find it. What am I doing wrong?

here is the code:

 import matplotlib.pyplot as pyplot ax = fig.add_subplot(111) fig = pyplot.figure() x_axis = [0, 1, 2, 3, 4, 5] y_axis = [334, 350, 385, 40000.0, 167000.0, 1590000.0] ax.bar(x_axis, y_axis, log = 1) pyplot.show() 

I get an error even when uninstalling pyplot.show. Thank you in advance for your cooperation

+4
source share
3 answers

The error occurs due to the log = True operator in ax.bar(... I'm not sure if this is a matplotlib error or it is used unintentionally. It can be easily fixed by removing the log=True argument argument.

This can simply be fixed by simply registering the y values ​​yourself.

 x_values = np.arange(1,8, 1) y_values = np.exp(x_values) log_y_values = np.log(y_values) fig = plt.figure() ax = fig.add_subplot(111) ax.bar(x_values,log_y_values) #Insert log=True argument to reproduce error 

Corresponding log(y) labels must be added for cleaning, these are log values.

+1
source

Are you sure this is your code? Where does the code throw an error? During the plot? Because it works for me:

 In [16]: import numpy as np In [17]: x = np.arange(1,8, 1) In [18]: y = np.exp(x) In [20]: import matplotlib.pyplot as plt In [21]: fig = plt.figure() In [22]: ax = fig.add_subplot(111) In [24]: ax.bar(x, y, log=1) Out[24]: [<matplotlib.patches.Rectangle object at 0x3cb1550>, <matplotlib.patches.Rectangle object at 0x40598d0>, <matplotlib.patches.Rectangle object at 0x4059d10>, <matplotlib.patches.Rectangle object at 0x40681d0>, <matplotlib.patches.Rectangle object at 0x4068650>, <matplotlib.patches.Rectangle object at 0x4068ad0>, <matplotlib.patches.Rectangle object at 0x4068f50>] In [25]: plt.show() 

Here is the plot enter image description here

+7
source

As mentioned in the comments on Greg, you really see the problem that was fixed in matplotlib 1.3 by setting the default behavior on the clip. Upgrading to 1.3 fixes the problem for me.

Note that it doesn’t matter how you use the log scale, either as a keyword argument to bar or via set_yscale on the axis.

See also this answer to “Logarithmic y-axis boxes in python” , which offers this workaround:

 plt.yscale('log', nonposy='clip') 
+3
source

Source: https://habr.com/ru/post/1495015/


All Articles