Issue
I am using matplotlib.pyplot.scatter to draw some simple scatter plot. However, something wrong appears and I cannot find the solution. Here is the code for drawing this scatter plot:
# xActA, yActA, xActQ, yActQ are all lists with same dimensions.
ax1 = scatter(xActA, yActA, color = 'blue',s = 20, label = 'Answers', linestyle = 'o')
ax2 = scatter(xActQ, yActQ, color = 'black', s = 20, label = 'Questions', linestyle = 'o')
ax1.set_label('Answers')
ax2.set_label('Questions')
xscale('log')
yscale('log')
title('User activity')
xlabel('Number of posts')
ylabel('Number of users')
legend()
f1.show()
f1.savefig('figure7_test.png')
And there is no error but the plot does not contain any dots.
Here is the data:
xActA = [0, 1, 2, 3, 4, 5, 6, 129, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 147, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
35, 36, 39, 40, 7, 45, 46, 49, 50, 52, 53, 183, 59, 63, 65, 69,
70, 72, 73, 55, 77, 78, 84, 85, 43, 215, 88, 100, 94, 131, 167,
19, 375, 122, 125, 149]
len(xActA) = 70
yActA = [1212, 822, 194, 94, 61, 44, 24, 1, 26, 20, 11, 16, 10, 8, 5, 8,
5, 5, 3, 1, 4, 4, 5, 3, 2, 3, 4, 3, 1, 2, 2, 3, 2, 1, 2, 2, 2, 2,
31, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1]
len(yActA) = 70
xActQ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 29, 36, 40, 45, 48, 50, 55, 67, 124]
len(xActQ) = 34
yActQ [204, 242, 150, 50, 49, 27, 5, 9, 4, 2, 6, 3, 2, 8, 4, 5, 1, 3, 3, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
len(yActQ) = 24
And the second error is raised by using another data set. I am checking the data...Sorry for the previous unclear description.
Solution
If you look closer at your data you will notice that most of your data points fall outside of the plot area (x=[1e2:1e3]; y=[1e2:1e4]). If you use a linear scale instead of a logarithmic scale you will see some scatter points, however, not in a very readable manner. But if you change your scale to 'symlog' and then set the x and y limits using 'xlim' and 'ylim', your done. Of course you have to make sure that the marker is set to 'o' when running 'scatter'. Check the full code.
from pylab import *
# The data
xActA = array([0, 1, 2, 3, 4, 5, 6, 129, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 147, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36,
39, 40, 7, 45, 46, 49, 50, 52, 53, 183, 59, 63, 65, 69, 70, 72, 73, 55, 77,
78, 84, 85, 43, 215, 88, 100, 94, 131, 167, 19, 375, 122, 125, 149])
yActA = array([1212, 822, 194, 94, 61, 44, 24, 1, 26, 20, 11, 16, 10, 8, 5, 8,
5, 5, 3, 1, 4, 4, 5, 3, 2, 3, 4, 3, 1, 2, 2, 3, 2, 1, 2, 2, 2, 2, 31, 2, 1,
1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2,
1, 1, 1, 1])
xActQ = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 29, 36, 40, 45, 48, 50, 55, 67, 124])
yActQ = array([204, 242, 150, 50, 49, 27, 5, 9, 4, 2, 6, 3, 2, 8, 4, 5, 1, 3,
3, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
# The plots
close('all')
f1 = figure()
ax1 = scatter(xActA, yActA, color='blue', s=20, label='Answers', marker='o')
ax2 = scatter(xActQ, yActQ, color='black', s=20, label='Questions', marker='o')
xscale('symlog')
yscale('symlog')
xlim([0, 1e3])
ylim([0, 1.5e3])
title('User activity')
xlabel('Number of posts')
ylabel('Number of users')
legend()
f1.show()
f1.savefig('figure7_test.png')
And these couple of lines of code give you this neat figure:
(source: nublia.com)
Answered By - regeirk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.