Issue with changing Xlabel in DensityScatterChart fucntion
Show older comments
Hi,
I am using DensityScatterChart() function to color code the data points based on the density.
Unfortunately, I couldn't change the X label and colorbar, I inded tried the following
d.XLabel='pixels','fontweight','bold','FontSize',10,'Interpreter','latex';
However, it prints only pixels without taking taking into acount the other font properties.
Actully my aim is to color code the data points in the scatter plot based on the desnity. Any other alternatives also appreciable.
2 Comments
Cause it seems like that functionality is not implemented in DensityScatterChart class.
From the class the set xlabel method is:
function set.XLabel(obj,str)
obj.getAxes.XLabel.String = str;
end
So you can either extend the functionality yourself by modifying this method or you can do it outside of the class manually via the xlabel command.
MechenG
on 20 Aug 2024
Accepted Answer
More Answers (1)
Shubham
on 20 Aug 2024
Hi MechenG,
As per my understanding, you want to customize the appearance of the X-axis label and the colorbar in a 'DensityScatterChart' plot. Specifically you want to:
- Set the X-axis label to "pixels".
- Apply additional font properties to the X-axis label, such as "FontWeight", "FontHeight" and using LaTeX for interpreting text.
As @Aquatris told, it seems that the functionality to change additional font properties is not implemented in "DensityScatterChart" class. Below is a code script for creating a density scatter plot:
% Generate some example data
x = randn(100, 1);
y = randn(100, 1);
% Create a density scatter plot
d = densityScatterChart(x, y);
Below are 2 possible workarounds on how you can customize the X-axis label and additional font properties.
- Unmanage the "densityScatterChart" object to access the underlying axes. Refer to the code snippet below to do the same:
% Force MATLAB to render the plot before making further changes
drawnow;
% Unmanage the DensityScatterChart object to access the underlying axes
[tcl, ax, scat] = unmanage(d);
% Now, set the font properties using the axes handle
ax.XLabel.String = 'pixels';
ax.XLabel.FontWeight = 'bold';
ax.XLabel.FontSize = 10;
ax.XLabel.Interpreter = 'latex';
ax.YLabel.String = 'Density';
ax.YLabel.FontWeight = 'bold';
ax.YLabel.FontSize = 10;
ax.YLabel.Interpreter = 'latex';
2. You can also modify the colorbar or other properties through the axes. To achieve this, refer to the code snippet below:
% Force MATLAB to render the plot before making further changes
drawnow;
% You can also modify the colorbar or other properties through the axes
set(ax, 'FontName', 'Times New Roman', 'FontSize', 25);
You can also refer to the Discussions tab of the following MathWorks File Exchange link for more information:
Hope this helps.
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!