It should be noted that these values are relative to other items on the plot.įor the grid we want the zorder value to be less than the value we use for the scatter plot. This controls the order in which components of the chart are plotted. To plot the grid lines behind, we need to move the plt.grid() line so that it is before the call to plt.scatter() and add in the parameter for zorder. Matplotlib scatter plot after adding gridlines. However, when we do this we will find that the grid lines appear on top of our plot, and it does not look visually appealing. plt.figure(figsize=(10,8)) plt.scatter(df, df, c=df, vmin=0, vmax=100, cmap='viridis_r') plt.xlim(0, 0.8) plt.ylim(0, 0.8) plt.colorbar(label='Gamma Ray (API)') plt.gca().t_visible(False) plt.gca().t_visible(False) plt.title('Density Porosity vs Neutron Porosity Scatter Plot', fontsize=14, fontweight='bold') plt.xlabel('Neutron Porosity (dec)') plt.ylabel('Density Porosity (dec)') plt.grid() plt.show() To do this we need to add in plt.grid() to our code. In this example, we will add some faint gridlines so that they do not detract too much from the data. For example, if you just want to show general trends within a dataset and don’t want the reader to focus too much on the raw values. However, there are times when grid lines are considered “junk” and they are best left off. ![]() This is especially important if we want to quantitively extract values from the plot. Adding a Faint Grid (Optional)ĭepending on the purpose of the plot, we may want to add a grid to so that readers of the chart can visually and easily navigate the plot. Matplotlib scatter plot after adding a title and labels to the axes. Right away we know what is plotted on the axes, what the chart is about and what the colour range represents. ![]() When we run the above code we get the following plot. plt.figure(figsize=(10,8)) plt.scatter(df, df, c=df, vmin=0, vmax=100, cmap='viridis_r') plt.xlim(0, 0.8) plt.ylim(0, 0.8) plt.colorbar(label='Gamma Ray (API)') plt.gca().t_visible(False) plt.gca().t_visible(False) plt.title('Density Porosity vs Neutron Porosity Scatter Plot', fontsize=14, fontweight='bold') plt.xlabel('Neutron Porosity (dec)') plt.ylabel('Density Porosity (dec)') plt.show() This helps readers to understand the plot better. Within each of these we pass in the text we want to appear and any font attributes such as font size.Īlso, it is good practice to include the units of measurement in the label. These can simply be added by using: plt.xlabel, plt.ylabel, and plt.title. When looking at the above scatter plot, we may know what each axis represents, but how are others going to understand what this plot is about, what the colours represent and what is plotted against what?Īdding a title and axis labels to our plot is an essential part of creating effective visualisations. Matplotlib scatter plot after removing right and top spines (edges). When we run the above code, we get back the following plot. This colour map provides a nice contrast between high and low values, whilst maintaining uniformity and being colour blind friendly. The colour map we are using is Viridis in reverse. This will make both axes start from 0 and go to a maximum of 0.8. įinally, we will set the x and y limits of the chart to go from 0 to 0.8 by calling upon plt.xlim() and plt.ylim(). We will need to display the colour bar by using plt.colorbar(). To do that we are going to colour the data by gamma ray (GR) and set the colour range between 0 and 100 ( vmin and vmax parameters). ![]() Looks very bland, doesn’t it? Let’s add some colour to make it more visually appealing and to allow us to gain some insight into the data. With these two lines of code, we get the above plot. When we run the above code we get back the following summary of the data with our new DPHI column. import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('data/Xeek_Well_15-9-15.csv') df = (2.65 - df)/1.65 df.describe() ![]() We will also calculate a density porosity column which we will be using to plot against neutron porosity. The data is then read into a dataframe using the pandas method read_csv(). įor this tutorial, we will need to import both matplotlib and pandas. The full dataset can be accessed at the following link. It is released under a NOLD 2.0 licence from the Norwegian Government, details of which can be found here: Norwegian Licence for Open Government Data (NLOD) 2.0. In the following examples of how a scatter plot can be enhanced within matplotlib we will be using a subset of a larger dataset that was used as part of a Machine Learning competition run by Xeek and FORCE 2020 (Bormann et al., 2020). Before and after enhancing a matplotlib figure.
0 Comments
Leave a Reply. |