Issue with changing Xlabel in DensityScatterChart fucntion
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Aquatris
il 20 Ago 2024
Modificato: Aquatris
il 20 Ago 2024
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.
Risposta accettata
Malay Agarwal
il 20 Ago 2024
You can set the x-label using all the properties by first calling the "unmanage" function on the "densityScatterChart" object. The "unmanage" function is provided as part of the API for the "densityScatterChart" class:
function [tcl,ax,scat] = unmanage(obj)
% UNMANAGE a densityScatterChart
%
% UNMANAGE(dsc) - transforms the densityScatterChart dsc into a (regular)
% scatter object in an axes in a 1x1 TiledChartLayout. This operation is
% permenant and prevents any further access to original interface of the
% densityScatterChart, including its properties and methods.
%
% tcl = UNMANAGE(dsc) returns the TiledChartLayout. Use this object to
% manage the position of the unmanaged densityScatterChart
%
% [tcl, ax] = UNMANAGE(dsc) also returns the Axes. Use the axes to
% customize details like grid or fontsize, or as a target for other plotting
% commands.
%
% [tcl, ax, scat] = UNMANAGE(dsc) also returns the Scatter. Use the Scatter
% to customize details like the Marker or MarkerSize.
%
% Use UNMANAGE to remove the contents of a densityScatterChart so that you
% can alter detailed aspects of the display or add additional graphics to
% the axes.
%
% UNMANAGE will disable access to the managed, high-level, interface and
% instead allow direct access to the graphics components.
% <strong> Note: this action is irreversible!<strong>
%
% A densityScatterChart is a standalone visualization, providing an
% abstract set of controls that display a scatter chart. This allows you to
% set various properties and have those settings reflected in the
% underlying scatter object. However, this framework can limit the ability
% to manipulate densityScatterCharts to the set of properties that
% densityScatterChart provides.
drawnow
if nargout>0
tcl = obj.getLayout;
end
if nargout>1
ax = obj.getAxes;
end
if nargout>2
scat = obj.Scat;
end
obj.getLayout.Parent = obj.Parent;
delete(obj)
end
This will convert it into a regular scatter object and you can use it like any other plot in MATLAB. For example:
x = randn(1000,1);
y = randn(1000,1);
d = densityScatterChart(x, y, 'UseColor', false, 'UseAlpha', true);
ax = unmanage(d);
xlabel(ax, "pixels", 'fontweight','bold','FontSize',10, "Color", "red", 'Interpreter','latex')
Hope this helps!
Più risposte (1)
Shubham
il 20 Ago 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Scatter Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!