filmov
tv
Python Tutorial: Customizing your plots

Показать описание
---
Now that you know how to add data to a plot, let's start customizing your plots.
First let's customize the appearance of the data in the plot.
Here is the code that you previously used to plot the data about the weather in Seattle.
One of the things that you might want to improve about this plot is that the data appears to be continuous, but it was actually only measured in monthly intervals. A way to indicate this would be to add markers to the plot that show us where the data exists and which parts are just lines that connect between the data points.
The plot method takes an optional keyword argument, marker, which lets you indicate that you are interested in adding markers to the plot and also what kind of markers you'd like. For example, passing the lower-case letter "o" indicates that you would like to use circles as markers.
If you were to pass a lower case letter "v" instead, you would get markers shaped like triangles pointing downwards.
To see all the possible marker styles, you can visit this page in the Matplotlib online documentation.
In these versions of the plot, the measured data appears as markers of some shape, and it becomes more apparent that the lines are just connectors between them.
But you can go even further to emphasize this by changing the appearance of these connecting lines.
This is done by adding the linestyle keyword argument. Here two dashes are used to indicate that the line should be dashed. Like marker shapes, there are a few linestyles you can choose from, listed in this documentation page.
You can even go so far as to eliminate the lines altogether, by passing the string "None" as input to this keyword argument.
Finally, you can choose the color that you would like to use for the data.
For example, here we've chosen to show this data in red, indicated by the letter "r".
Another important thing to customize are the axis labels. If you want your visualizations to communicate properly you need to always label the axes. This is really important but is something that is often neglected.
In addition to the plot method, the Axes object has several methods that start with the word set. These are methods that you can use to change certain properties of the object, before calling show to display it.
For example, there is a set-underscore-xlabel method that you can use to set the value of the label of the x-axis. Note that we capitalize axis labels as we would capitalize a sentence, where only the first word is always capitalized and subsequent words are capitalized only if they are proper nouns.
If you then call plt-dot-show you will see that the axis now has a label that indicates that the values on the x-axis denote time in months.
Similarly, a set-underscore-ylabel method customizes the label that is associated with the y-axis. Here, we set the label to indicate that the height of the line in each month indicates the average temperature in that month.
Finally, you can also add a title to your Axes using the set-underscore-title method. This adds another source of information about the data to provide context for your visualization.
OK. Now that you have seen some examples of customizing the appearance of the data in your plots, and the axis labels, it's time to get a bit of practice with these concepts.
#Python #PythonTutorial #DataCamp #Data #Visualization #Matplotlib #Customizing #plots