Learn how to set plot formatting—colors, styles, and markers.
- [Instructor] It's time to talk about formatting your plot. Well-formatted plots are easier to interpret at a passing glance. In this section, I'm going to show you how to define custom plot colors, line styles, and marker styles. The reason I think this is important to show you is that enhancing chart colors and markers can help you convey a point in a way that is visually easier to understand. Just think. If all the slices of a pie chart had the same color, the chart would be meaningless. And if all the lines in the line chart were the same color and used the same markers, you wouldn't be able to decipher one from the other and the chart would have no meaning.
Matplotlib offers colors, line styles, and marker style options to help you build clarity into your data visualization. The plotting library has colors specified out by name, where you can just write in the name of the color that you want or for even more customization, you can just pass in the hex code of the color you'd like to use. Matplotlib also offers custom line styles, so you can set custom styles and widths for each of your lines in a line plot.
If you look at this little chart here, you can see some of the codes you would use to set those styles. And I'm also going to show you this in the demonstration. You can also set custom marker styles. I created this handy dandy little chart for you to review. In case you want to get fancy with your charts, you can look that over, but I'm going to show you how to customize your markers in just a second. Now that you know how to generate your plots, let's look at how to customize them. In this demonstration, we're going to be using numpy and pandas again.
So, we'll import those libraries. Let's also import matplotlib, like we have been. But for this demonstration, we're going to be using seaborn, so we want to make sure to bring that in. We'll say import seaborn as sb and execute this code. Then we have our libraries. We will set our parameters for our data visualizations, like we always do. Pace that in, run it. And we need to create some objects to plot.
So, we'll create an x variable and a y variable. X will be a series of numbers between one and nine. And y is going to be a list of numbers: one, two, three, four, 0.54321. And I just need to go back through and add the commas in. Okay, we have two objects. Now, to create a bar chart, you can just call the bar function and pass in the x and y objects.
So, that will be plt.bar. And then, pass an x and y. And we have a horizontal bar chart. Now, let me show you how to adjust the color, bar width, and bar alignment. Now, let's create a list called wide and pass in all of the widths we want for each bar in our bar chart. So, well say wide is equal to. And then our first bar will be a width of 0.5. Next one will be 0.5.
And then, in the middle, we'll say we want the width to be 0.9, a little wider in the middle. We'll do that a few times and then we just need to make sure we've got the same number of parameters here as bars in our bar chart. So, we've got one, two, three, four, five, six, seven, eight, nine. And do we have nine parameters here? One, two, three, four, five, six, seven, eight, nine. Perfect. And then, let's also set a custom color for our bar chart and we'll just call that salmon.
So, we just type out a string that says salmon inside of a list. And to plot it out, we say plt.bar. We pass in x and y, our two objects. And then, we'll pass in a width parameter and say width is equal to wide. And color is equal to color. Lastly, I want to show you how to set the alignment of your bar charts. You can just pass in an argument that says align. And then, we'll say that we want it centered.
Run this. And you can see that the widths have been changed as well as the colors and the centering. Let's look at how to customize colors when you're plotting pandas objects. So, as far as our pandas object, we're going to use the mtcars data set, like we have been. So, we'll just load that in. And I'm going to create a subset to make it easier to visualize our plotting here. So, we'll call that subset df and we'll use the variables cylinders, mpg, and weight.
And then, I'll just say df.plot to plot it out. And here we go. We've got a line chart of our subset, our cylinders variable, mpg, and weight. Now we want to show you how to customize the colors of this. Let's just make another little list here and we'll call it color theme and then we're going to set a color theme for it. And we'll use the colors dark gray... I got these colors from that chart I just showed you.
Light salmon, powder blue. So, we need to pass in a color for each variable we're plotting. We have three variables. So, next we're going to call the dot plot method off of our df data frame and we're going to pass in parameter color equal to color theme. So, we do that by saying df.plot. And then, our parameter for color is going to be color theme that we just created.
And now you can see that the colors have been customized. That was pretty easy. I just want to show you how to customize a pie chart as well. For this, we'll just create a separate list object. We'll call it z. And we'll say one, two, three, four, and 0.5. These are our different elements that will be plotted in our pie chart. And then, to plot it, we call plt.pie, pass in our z object. And then to print, we say plt.show.
And here's a standard pie chart. Now, to customize the colors. This time I'm going to show you how to use the hex codes to customize colors. So, we'll say... We'll create a list called color theme and then we're going to name all of the hex codes for the colors we want to use in this pie chart. So, for the first color, it'll be #A9A9A9. And then the next color would be FFA07A.
We need five colors, so let's just add in these last three. And then, what do we do with this color theme? Well, we can just take our plt.plot function here that we used to create the original pie chart. And then, we just add in colors equal to color_theme. And let me point out that the parameter here for colors is different than in when we plotted colors for our data frame cause that parameter's color.
And we just print this out and we get our custom colors, but we generated those with hex codes instead of the named colors. I wanted to point that out cause it's a little different. When we did the data frame, it had a different name for the parameters and it's important just to get practice with the different types of objects you plot and the different types of plots you generate. The syntax is a little different. Moving onto custom line styles. First, I'll make a few objects we can use for plotting.
So, let's say x1. Make that a series of numbers between zero and 10. And then, an object called y1. And this will be a list of numbers, 10 and nine. And then we'll just have a list of numbers between 10 and one in descending order. And we'll create a line plot with two lines. To do that, you would just call plt.plot and plot the first line.
In this example, let's plot our x and y variable that we created earlier in this demonstration. And then, we'll plot a second line and we'll call that... We'll plot the x1 and y1 variables we just created. And then, run this. And you see we've got two lines. Now I just want to show you really quick how to customize them. So, I will copy this code and then just pass in some new arguments, the line style to be steps.
We would just set line style equal to a string that says steps and the line width, lw, is equal to five here. And then, in the second line, let's say a line style, the ls argument, is going to be equal to double dash. So, we just make a string and we pass in two dash marks. And we'll make our line width where the lw parameter equal to 10.
And then we can just run that and you see that our chart has been significantly changed. The blue line has now been changed to a steps line style. And the green line has now been given dashes. And also, the widths have been changed. The last thing I want to show you really quick is how to change marker styles. So, let's just take this same code here and we're going to add some markers to these lines for each of the vertices in this line chart.
For that, we use the marker parameter and a parameter called mew. So, marker the first line will make that equal to one. Now, you pull these codes from the provided table with all the codes for the different styles. So, you can just pick and choose between them. Mew, it specifies the marker width. We'll say 20 here. And then, for the second line, we'll say... We want to use the plus marker style and a width of 15.
And you plot these out. And you can see that now markers have been added to our line chart and they've been customized with custom styles and sizes. This should be useful when you're trying to make your chart really easy to read at a glance. You can customize the lines to help people understand. Another way to help people understand your data visualization at a glance is to add labels and annotations. We're going to cover that next.
- Getting started with Jupyter Notebooks
- Visualizing data: basic charts, time series, and statistical plots
- Preparing for analysis: treating missing values and data transformation
- Data analysis basics: arithmetic, summary statistics, and correlation analysis
- Outlier analysis: univariate, multivariate, and linear projection methods
- Introduction to machine learning
- Basic machine learning methods: linear and logistic regression, Naïve Bayes
- Reducing dataset dimensionality with PCA
- Clustering and classification: k-means, hierarchical, and k-NN
- Simulating a social network with NetworkX
- Creating Plot.ly charts
- Scraping the web with Beautiful Soup