Power BI simple custom line chart with Python visual and matplotlib
Step-by-step instruction:
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.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:
y1for the current year's profit,y2for the previous year's profit, andy3for the profit growth or decline. Make sure to accurately identify and assign the corresponding columns from your dataset to these variables.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 withy1by usingplt.plot(x, y1)wherexrepresents the x-axis values andy1represents 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 thecolorparameter as 'tomato'. Repeat the same steps fory2andy3, adjusting the marker style and color as desired.Once the plots are defined, display the chart using the
plt.show()function.Next, you need to define the values that indicate growth and decline in the
y3variable. Create two variables,lowandhigh, to represent the values for decline and growth, respectively. Use conditions to assign values to these variables based on they3values. For example,low = y3[y3 <= 0]would select the values iny3that are less than or equal to zero, indicating decline, whilehigh = y3[y3 >= 0]would select the values greater than or equal to zero, representing growth.Update the plots for the
y3variable to include markers that indicate growth or decline. You will now need two separate plots fory3. For the decline values (low), useplt.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), useplt.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.Execute the code to display the updated chart with proper markers for growth and decline.
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 fory3growth (g) using thezip()function. Inside the loop, useplt.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 thexandycoordinates. Adjust the font size and alignment as desired.Run the code to display the chart with the added data labels.
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.Run the code to display the final chart with the legend. Adjust the font size of the legend if necessary.
Комментарии
Отправить комментарий