How To Represent Very Large And A Very Small Values In A Plot
I need to plot 3 values in a histogram. One of them is a very large value compared to the other ones. When I try to plot them, because of the large one other two values do not sh
Solution 1:
- Use
matplotlib.pyplot.yscale('log')
ormatplotlib.axes.Axes.set_yscale('log')
plt.yscale('log')
orax.set_yscale('log')
'symlog'
if there are negative values.
- Many parameters can be set in
matplotlib.axes.Axes.set
ax.set(yscale='log')
- This solution is relevant for
matplotlib
,seaborn
axes level plots, andpandas
plots.
Imports and Data
import matplotlib.pyplot as plt
import numpy as np
height = [0.422602, 0.000011, 0.000453]
bars = ('2X2', '4X4', '8X8')
y_pos = np.arange(len(bars))
Example 1
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
ax1.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
ax1.set(xlabel='Matrix Dimensions', ylabel='Fidelity for Matrices with Sparsity 1', title='y without log scale')
ax1.set_xticks(y_pos)
ax1.set_xticklabels(bars)
ax2.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
# set yscale; can also use plt.yscale('log') or plt.yscale('symlog')
ax2.set(yscale='log', xlabel='Matrix Dimensions', ylabel='Fidelity for Matrices with Sparsity 1', title='y with log scale')
ax2.set_xticks(y_pos)
ax2.set_xticklabels(bars)
fig.tight_layout()
plt.show()
Example 2
plt.bar(y_pos, height, color = (0.572549,0.2862,0.0,1))
plt.yscale('log')
plt.xlabel('Matrix Dimensions')
plt.ylabel('Fidelity for Matrices with Sparsity 1')
plt.xticks(y_pos, bars)
plt.show()
Solution 2:
This is not a problem of the bar itself, as differences of the number are just too big to be displayed.
In this case usually logarithmic axes are used. This means you do not have a linear axis, but a logaritmic one. See the documentation here: https://matplotlib.org/3.1.0/gallery/scales/log_bar.html
Post a Comment for "How To Represent Very Large And A Very Small Values In A Plot"