0

I currently have three seperate scatterplots showing with the same x and y axis (with the 3rd dimension being the color coding) but they all follow each other downards, one graph below the other. Here is the code I use for that (in this case I have 3 scatterplots:

fig, ax = plt.subplots(figsize=(10,10))
plt.xlim(0,2000000)
plt.ylim(0,250000)
ax.scatter(Sales_Force.Revenues[hello], Sales_Force.Amount[hello], 
alpha=0.05, color='g')
ax.set_title("Revenue vs Amount for Booked")
ax.set_xlabel("Revenue", size = 10)
ax.set_ylabel("Amount", size = 10)
plt.xticks(fontsize=10)
plt.show()

Afterwards, I have 2 other versions of almost identical code replacing the "hello" with 2 other words, hence creating me 3 separate scatterplots. My question is, how (and preferably in as simple of a modification as possible, if possible) can I alter my code in order to keep the exact same plots but them being next to each other side by side instead of one below another?

I also tried doing this based on another answer I saw on stackoverflow. I tried this:

fig, axes = plt.subplots(1,2)

plt.xlim(0,2000000)
plt.ylim(0,250000)
ax1.scatter(Sales_Force_Before_Merger.Revenues[hello], 
Sales_Force_Before_Merger.Amount[hello], alpha=0.05, color='g')
ax1.set_title("Revenue vs Amount for Booked Before Merger")
ax1.set_xlabel("Revenue", size = 10)
ax1.set_ylabel("Amount", size = 10)
ax1.plt.xticks(fontsize=10)
ax1.plt.show(ax=axes[0])

plt.xlim(0,2000000)
plt.ylim(0,250000)
ax2.scatter(Sales_Force_Before_Merger.Revenues[goodbye], 
Sales_Force_Before_Merger.Amount[goodbye], alpha=0.05, color='r')
ax2.set_title("Revenue vs Amount for Booked Before Merger")
ax2.set_xlabel("Revenue", size = 10)
ax2.set_ylabel("Amount", size = 10)
ax2.plt.xticks(fontsize=10)
ax2.plt.show(ax=axes[1])

After I do this I get an error message stating:

AttributeError: 'AxesSubplot' object has no attribute 'plt'

and have two graphs showing with nothing in them (some minor modification might have populated one of the 2 plots) and additionally, both the non populated scatterplots also have different x and y value ticks showing even though they are supposed to be showing the same data.

bernando_vialli
  • 947
  • 4
  • 12
  • 27
  • I have already looked at that question and its answers, I don't know how to apply anything from there to my question. – bernando_vialli Mar 01 '18 at 14:12
  • Because obviously `ax1` has no attribute `plt` (why should it?), the solution is just as in the duplicate to use `plt` without trying to make it an attribute of something else. – ImportanceOfBeingErnest Mar 01 '18 at 14:15
  • 1
    Note that `plt.subplots` creates a new figure. If you want subplots, don't use `plt.subplots` several times. Also, `plt.show()` does not take any arguments and should be called once at the end, not somewhere in between the lines. – ImportanceOfBeingErnest Mar 01 '18 at 14:21
  • ok I got rid of the argument in plt.show() and kept only one of them. However, I really don't follow the argument in that duplicated post. I don't understand what they are doing in that for loop and how that relates to what I have – bernando_vialli Mar 01 '18 at 14:24
  • like what is the nrows=2, ncolumns=2? I don't get how rows and columns equal 2 relate to anything? – bernando_vialli Mar 01 '18 at 14:25
  • 1
    Well, while I'm not sure what is unclear about that (`nrows` are the number of subplot rows of your plot, `ncols` are the number of columns; here you want one row and three columns), *that* would actually be a question you can ask here - although make sure it hasn't also been asked before. – ImportanceOfBeingErnest Mar 01 '18 at 14:28
  • ok finally I think I got it to work. Thanks! – bernando_vialli Mar 01 '18 at 14:45
  • follow up question, is there a maximum number of plots I can put side by side. I get a ValueError: num must be 1 <= num <= 3, not 4 message when I tried to do 7, but can't find anything online that says there is a maximum – bernando_vialli Mar 01 '18 at 19:59
  • No, there is no maximum number of subplots. (just as you reach 100 or so you may start getting problems with the visualization) – ImportanceOfBeingErnest Mar 01 '18 at 20:04
  • so why is it giving me an error when I did more then 3? Just to clarify, I don't mean the number of rows, I am referring to the number of columns. – bernando_vialli Mar 01 '18 at 20:31
  • Nobody can know, because "I did more then 3" is not a clear problem description. – ImportanceOfBeingErnest Mar 01 '18 at 20:34
  • I did this 10 times and was hoping to see all 10 plots in 1 row: plt.subplot(1, 3, 10) plt.scatter(Sales_Force_Before_Merger.Revenues[Withdrawn], Sales_Force_Before_Merger.Amount[Withdrawn],alpha=0.5, color='lightgrey') plt.xlim(0,2000000) plt.ylim(0,150000) plt.title("Revenue vs Amount for Cancelled Before Merger") plt.xlabel("Revenue", size = 15) plt.ylabel("Amount", size = 15) – bernando_vialli Mar 01 '18 at 20:35
  • A subplot grid with 1 row and 3 columns has 3 subplots. Naturally there is no tenth subplot, if there are only 3 present. – ImportanceOfBeingErnest Mar 01 '18 at 20:37
  • I can't figure this out. I used this code that you suggested on multiple occasions the past few weeks and it was working fine (regarding subplots). Now when I do the identical thing on a different pandas dataframe, I get (I am not doing anything differently than I did before): AttributeError: 'numpy.ndarray' object has no attribute 'scatter' – bernando_vialli Mar 13 '18 at 20:09
  • This error means that you are trying to call `axarray.scatter()` where `axarray` is an array of axes instead of an axes itself. – ImportanceOfBeingErnest Mar 13 '18 at 20:13
  • Not sure I fully understood what that meant? What would be the reason for this? – bernando_vialli Mar 13 '18 at 20:14
  • The reason would be that you forgot to get the correct axes out of some returned object. E.g. if `axarray = [ax1,ax2]` you try to call `axarray.scatter` instead of `axarray[0].scatter`. – ImportanceOfBeingErnest Mar 13 '18 at 20:18

0 Answers0