Bargraph相对于固定点Matplotlib

时间:2015-12-19 20:47:02

标签: python excel matplotlib

我试图绘制一组相对于1的点;我可以在Excel上执行此操作,但我很难在Python中执行此操作。对我来说最困难的部分是将其保持为Y轴的对数刻度。如果我有价值

0.15 0.7 1.3 0.5 1.7,

如何使用matplotlib达到如下所示的相同效果(由Excel完成)?到目前为止,我提出的最好的方法是从每个值中减去1以实际将其置于0左右,但这最终会使缩放变得混乱。

这就是我想要的:

This is the Excel version that I want to replicate in Python

这是我从Python获得的失败尝试: enter image description here

Python的另一个失败的尝试给了我这个;即使Y轴在对数范围内,也要注意一切从底部开始,当我真的希望值相对于中心上升/下降时,或者在Y轴上上升{1 }}

1 个答案:

答案 0 :(得分:0)

这适合你吗?

# our imports
import numpy as np
import matplotlib.pyplot as plt


# define the sample size and draw a random sample
N = 5
ind = np.arange(N)
sample = np.random.uniform(low=-1, high=1, size=(N))

# initialize our bar width and the subplot
width = 0.35
fig, ax = plt.subplots()

# plot our indexes, sample using a blue color
rects1 = ax.bar(ind, sample, width, color='blue')

# Set our axes labels, title, tick marks, and then our x ticks.
ax.set_ylabel('Scores')
ax.set_title('Scores Example')
ax.set_xticks(ind + width)
ax.set_xticklabels(('E1', 'E2', 'E3', 'E4', 'E5'))

# Create a horizontal line at the origin
ax.axhline(y=0, color='black')

# Show our plot, do whatever
plt.show()

MPL Example

可以找到更多示例和参考资料herehere

您也可以使用十六进制代码并为条形图设置无轮廓,并为实际绘图提供一些间距,同时添加对数轴:

这是给出类似于您自己的代表的最终代码:

# our imports
import numpy as np
import matplotlib.pyplot as plt


# define the sample size and draw a random sample
N = 5
ind = np.arange(N)
sample = 10 ** np.random.uniform(low=-2, high=2, size=(N))

# initialize our bar width and the subplot
width = 0.35
fig, ax = plt.subplots()

# plot our indexes, sample using a hex color and a 0 linewidth to get rid
# of the plot edges
rects1 = ax.bar(ind, sample, width, color='#50A6C2', linewidth=0)

# Set our axes labels, title, tick marks, and then our x ticks.
ax.set_ylabel('Scores')
ax.set_title('Scores Example')
ax.set_xticks(ind + width)
ax.set_xticklabels(('E1', 'E2', 'E3', 'E4', 'E5'))

# Create a horizontal line at the origin
ax.axhline(y=1, color='black')
# Set our limits
ax.set_xlim(-1, 5)
ax.set_yscale('log')
ax.set_ylim(0.01, 100)

# Show our plot, do whatever
plt.show()

MPL With Log