Most of the customizations and advanced uses you’ll learn about in this tutorial are only possible when using plt.scatter(). If you can create scatter plots using plt.plot(), and it’s also much faster, why should you ever use plt.scatter()? You’ll find the answer in the rest of this tutorial. When running the example above on my system, plt.plot() was over seven times faster. The performance will vary on different computers, but when you run this code, you’ll find that plt.plot() is significantly more efficient than plt.scatter(). timeit ( "plt.plot(price, sales_per_day, 'o')", number = 1000, globals = globals (), ), ) timeit ( "plt.scatter(price, sales_per_day)", number = 1000, globals = globals (), ), ) print ( "plt.plot()", timeit. Import timeit import matplotlib.pyplot as plt price = sales_per_day = print ( "plt.scatter()", timeit. You can achieve the same scatter plot as the one you obtained in the section above with the following call to plt.plot(), using the same data: Matplotlib’s plt.plot() is a general-purpose plotting function that will allow you to create various different line or marker plots. You can also produce the scatter plot shown above using another function within matplotlib.pyplot. You can then carry out further analysis, whether it’s using linear regression or other techniques. When using scatter plots in this way, close inspection can help you explore the relationship between variables. However, the drink that costs $4.02 is an outlier, which may show that it’s a particularly popular product. This plot shows that, in general, the more expensive a drink is, the fewer items are sold. In this tutorial, all the examples will be in the form of scripts and will include the call to plt.show(). When you’re using an interactive environment, such as a console or a Jupyter Notebook, you don’t need to call plt.show(). As you’re using a Python script, you also need to explicitly display the figure by using plt.show(). You then create lists with the price and average sales per day for each of the six orange drinks sold.įinally, you create the scatter plot by using plt.scatter() with the two variables you wish to compare as input arguments. This alias is generally used by convention to shorten the module and submodule names. In this Python script, you import the pyplot submodule from Matplotlib using the alias plt. Import matplotlib.pyplot as plt price = sales_per_day = plt. You don’t need to be familiar with Matplotlib to follow this tutorial, but if you’d like to learn more about the module, then check out Python Plotting With Matplotlib (Guide). To get the most out of this tutorial, you should be familiar with the fundamentals of Python programming and the basics of NumPy and its ndarray object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |