Reputation: 3316
I needed to plot a bar chart in Python. I wrote a simple example code.
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
x_axis = [20, 32, 64, 96, 160, 192, 224, 288, 320, 352, 416, 512, 576, 704, 864, 1024, 1056, 1152, 1408, 1536, 1824, 1952, 4096, 4192, 4896, 5664, 6144, 6176, 6944, 12128, 12288, 13824, 15424, 16384, 16768, 28800, 39520, 49152, 64512, 73728, 114688, 147456, 444544, 451200, 1206400, 1453472, 2565504, 3833856, 7243776, 17350656, 26007552, 42844160]
y_axis = [1, 31, 32, 63, 7, 11, 1, 2, 1, 1, 1, 4, 6, 26, 1, 1, 4, 2, 5, 1, 1, 1, 1, 4, 1, 1, 1, 48, 1, 1, 3, 1, 1, 4, 51, 1, 2, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ax.bar(x_axis, y_axis, align='center')
ax.set_xticks(x_axis)
ax.set_xticklabels([i for i in x_axis])
fig.savefig('graph.png')
but the graph it generates is totally wrong. What is wrong in here.
Upvotes: 1
Views: 287
Reputation: 12417
Similar to Bazingaa's answer, but this shows all bars with the same size:
c=0.05
plt.bar(x_axis, y_axis, width=c*np.array(x_axis), color='b')
ax.set_xscale("log")
Upvotes: 0
Reputation: 39072
The problem is that your x-values ranges from order 10**1
to 10**7
. So whatever bars you plot are not visible because they are fine thin discrete peaks. In order to visualize them, I did two things: First, assign a width to the bars and second, use a logarithmic x-axis. Following are the lines modified followed by the resulting plot. I am not sure if that served your need but without logarithmic scale, I don't think you will be able to visualize the bars with such a widely spanned x-range. In the figure below, the bar width with not consistent because of the logarithmic scale again but this is to just show you what the problem is (too large range of x-values)
plt.bar(x_axis, y_axis, align='center', width=15)
ax.set_xscale('log')
Output
Upvotes: 1