[英]How to draw bar charts for very small values in python or matplotlib?
I drawn the comparison bar chart for very small values with the following code, 我使用以下代码绘制了比较条形图,以获取非常小的值,
import pandas as pd
import matplotlib.pyplot as plt
data = [[ 0.00790019035339353, 0.00002112],
[0.0107705593109131, 0.0000328540802001953],
[0.0507792949676514, 0.0000541210174560547]]
df = pd.DataFrame(data, columns=['A', 'B'])
df.plot.bar()
plt.bar(df['A'], df['B'])
plt.show()
Due to very small values I can't visualise the chart colour for the ('B' column) smaller value (eg 0.00002112) in the graph. 由于值太小,我无法可视化图表中(“ B”列)较小值(例如0.00002112)的图表颜色。
How can I modify the code to visualise smaller value(B column) colour in the graph? 如何修改代码以可视化图表中较小的值(B列)颜色? Thanks.. 谢谢..
A common way to display data with different orders of magnitude is to use a logarithmic scaling for the y-axis. 显示不同数量级数据的常见方法是对y轴使用对数缩放。 Below the logarithm to base 10 is used but other bases could be chosen. 在以10为底的对数以下,但可以选择其他底。
import pandas as pd
import matplotlib.pyplot as plt
data = [[ 0.00790019035339353, 0.00002112],
[0.0107705593109131, 0.0000328540802001953],
[0.0507792949676514, 0.0000541210174560547]]
df = pd.DataFrame(data, columns=['A', 'B'])
df.plot.bar()
plt.yscale("log")
plt.show()
Update: To change the formatting of the yaxis labels an instance of ScalarFormatter
can be used: 更新:要更改ScalarFormatter
标签的格式,可以使用ScalarFormatter
的实例:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
data = [[ 0.00790019035339353, 0.00002112],
[0.0107705593109131, 0.0000328540802001953],
[0.0507792949676514, 0.0000541210174560547]]
df = pd.DataFrame(data, columns=['A', 'B'])
df.plot.bar()
plt.yscale("log")
plt.gca().yaxis.set_major_formatter(ScalarFormatter())
plt.show()
You could create 2 y-axis like this: 您可以像这样创建2个y轴:
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
width = 0.2
df['A'].plot(kind='bar', color='green', ax=ax1, width=width, position=1, label = 'A')
df['B'].plot(kind='bar', color='blue', ax=ax2, width=width, position=0, label = 'B')
ax1.set_ylabel('A')
ax2.set_ylabel('B')
# legend
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
ax1.legend(h1+h2, l1+l2, loc=2)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.