Plotting with Dots Instead of Horizontal Lines and More Granular Y Axis Values
Introduction
In this article, we will explore how to modify a plot created using the popular Python data visualization library Matplotlib. Specifically, we will show how to replace horizontal lines with dots and increase the granularity of the y-axis values.
We will start by examining the original code provided in the Stack Overflow post. The goal is to create a scatter plot that displays the nlargest
values from the '# of Trades'
column as dots instead of horizontal lines.
Understanding the Original Code
The original code uses Matplotlib’s plotting function to display two columns (‘High’ and ‘Low’) of the dataframe df
. It then sets the major formatter for the y-axis to display values with two decimal places using ticker.FormatStrFormatter('%.2f')
.
To create horizontal lines for the nlargest
values, the code uses a loop to iterate over the five largest rows based on the '# of Trades'
column. For each row, it creates a red line (axhline(y=l['High'], color='r')
) and a blue line (axhline(y=l['Low'], color='b')
). Finally, plt.show()
is called to display the plot.
Creating a Scatter Plot
To achieve the desired output, we need to create a scatter plot instead of a line plot. A scatter plot displays points on a grid, allowing us to visualize multiple data points at once.
The modified code uses Matplotlib’s scatter
function to create two scatter plots: one for the ‘High’ column and another for the ‘Low’ column. The s
parameter is used to set the size of the dots. In this case, we use np.linspace(50, 10, 5)
to generate five values that will be used as the dot sizes.
However, we need to adjust the code further to make the dot sizes relative to the rank of the value in the nlargest
list. This can be achieved by using a loop to iterate over the rows with the largest values and adjusting the size of each dot accordingly.
Modifying the Code
The modified code uses a loop to create two scatter plots: one for the ‘High’ column and another for the ‘Low’ column. For each row, it calculates the distance from the origin using x
and sets the y-coordinate to the corresponding value in the ‘High’ or ‘Low’ columns. The size of the dot is adjusted based on its rank in the list.
Here’s the modified code:
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
axnum.yaxis.set_major_locator(ticker.MultipleLocator(.05))
x = np.linspace(0, len(df), 10)
for i, (idx, l) in enumerate(df.nlargest(5, '# of Trades').iterrows()):
plt.scatter(x, y=[l['High']]*len(x), color='r', marker='o', s=(5-i) * 5 + 5)
plt.scatter(x, y=[l['Low']]*len(x), color='b', marker='o', s=(5-i) * 5 + 5)
Customizing the Plot
We can further customize the plot by adding labels, titles, and other elements. For example, we can add a title to the plot using plt.title()
.
Here’s an updated code snippet:
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
axnum.yaxis.set_major_locator(ticker.MultipleLocator(.05))
x = np.linspace(0, len(df), 10)
for i, (idx, l) in enumerate(df.nlargest(5, '# of Trades').iterrows()):
plt.scatter(x, y=[l['High']]*len(x), color='r', marker='o', s=(5-i) * 5 + 5)
plt.scatter(x, y=[l['Low']]*len(x), color='b', marker='o', s=(5-i) * 5 + 5)
plt.title('Nlargest Values Plot')
plt.xlabel('# of Trades')
plt.ylabel('Price')
Conclusion
In this article, we explored how to modify a plot created using Matplotlib. Specifically, we showed how to replace horizontal lines with dots and increase the granularity of the y-axis values.
We modified the original code by creating two scatter plots instead of line plots. We then adjusted the size of each dot based on its rank in the nlargest
list.
Finally, we customized the plot by adding labels, titles, and other elements to make it more informative and visually appealing.
By following this example, you can create similar plots for your own data using Matplotlib.
Last modified on 2024-06-08