Power BI simple custom line chart with Python visual and matplotlib

Step-by-step instruction:

  1. To convert an existing line chart into a Python visual, you will utilize the matplotlib library. Select your visual and change its type to Python by selecting correspond button on the right panel. Begin by importing the necessary module with the following code: import matplotlib.pyplot as plt.

  2. Define the x-axis and y-axis variables for your chart. For the x-axis, you will use the "month name" column from your dataset. For the y-axis, you need to define three variables: y1 for the current year's profit, y2 for the previous year's profit, and y3 for the profit growth or decline. Make sure to accurately identify and assign the corresponding columns from your dataset to these variables.

  3. Proceed to define the plots for each y-axis variable using the plt.plot() function. You will create three separate plots, one for each variable. Begin with y1 by using plt.plot(x, y1) where x represents the x-axis values and y1 represents the y-axis values for the current year's profit. Set the marker style as 'o' and choose a color for the plot, such as red, by specifying the color parameter as 'tomato'. Repeat the same steps for y2 and y3, adjusting the marker style and color as desired.

  4. Once the plots are defined, display the chart using the plt.show() function.

  5. Next, you need to define the values that indicate growth and decline in the y3 variable. Create two variables, low and high, to represent the values for decline and growth, respectively. Use conditions to assign values to these variables based on the y3 values. For example, low = y3[y3 <= 0] would select the values in y3 that are less than or equal to zero, indicating decline, while high = y3[y3 >= 0] would select the values greater than or equal to zero, representing growth.

  6. Update the plots for the y3 variable to include markers that indicate growth or decline. You will now need two separate plots for y3. For the decline values (low), use plt.plot(x[low], y3[low], marker='v', color='tomato'), specifying downward-facing triangles as markers and the color as red. For the growth values (high), use plt.plot(x[high], y3[high], marker='^', color='green'), specifying upward-facing triangles as markers and the color as green. Ensure that the marker size for all plots is set to 12.

  7. Execute the code to display the updated chart with proper markers for growth and decline.

  8. In order to add data labels to the chart, you will use annotations and a for loop. Iterate through each month name (m) and the corresponding value for y3 growth (g) using the zip() function. Inside the loop, use plt.annotate() to add the annotation, providing the annotation text as a formatted string. Format the values to include commas, one decimal place, and divide them by 1000. Specify the position of the annotation using the x and y coordinates. Adjust the font size and alignment as desired.

  9. Run the code to display the chart with the added data labels.

  10. Finally, add a legend to the chart to identify the different lines. Use plt.legend() and provide the labels for each line, such as "Current Year Revenue," "Current Year Profit," "Last Year Profit," "Growth," and "Decline." Adjust the font size, location (e.g., upper left), and the number of columns (e.g., 3) for the legend as needed.

  11. Run the code to display the final chart with the legend. Adjust the font size of the legend if necessary.

Комментарии

Популярные сообщения из этого блога

Today's activity report #17