Change transparency of filled scatter when using 'errorbar' function

19 次查看(过去 30 天)
Hi there,
I have a very simple question: I was trying to plot errorbar figure using:
errorbar(x,y,Std, '-o', "MarkerSize",15,...
"MarkerEdgeColor",'k', "MarkerFaceColor", 'k', 'Color', 'k', 'LineStyle','-.',...
'DisplayName', 'Dataname');
What I need is to adjust the face transparency of the data point. However, it seems like the function 'errorbar' does not support this option: I tried 'MarkerFaceAlpha' but this is not recognized.
Could someone give advice?
Cheers!

采纳的回答

Umar
Umar 2024-10-20

Hi @Bo ,

You mentioned, “ What I need is to adjust the face transparency of the data point. However, it seems like the function 'errorbar' does not support this option: I tried 'MarkerFaceAlpha' but this is not recognized. Could someone give advice?

First of all, you should follow @dpb advice. I agree with his comments after experimenting with code.

Now, let me address your query regarding adjusting marker face transparency when using the errorbar function in MATLAB, it's important to note that errorbar indeed lacks direct support for transparency properties like MarkerFaceAlpha. Instead, you can achieve the desired effect using a workaround by utilizing the scatter function (again as referenced by @dpb) in combination with errorbar. First, use the errorbar function to plot your data along with error bars without markers. Then, use the scatter function to overlay markers at the same data points with the desired transparency settings. Here is how you can implement this:

% Sample data
x = linspace(0, 10, 10); % X data
y = sin(x);              % Y data
err = 0.1 * ones(size(y)); % Error values
% Step 1: Plot error bars without markers
figure; % Create a new figure
e = errorbar(x, y, err, 'k-.'); % Black dashed line for error bars
% Step 2: Overlay transparent markers
hold on; % Retain current plot
scatter(x, y, 100, 'k', 'filled', 'MarkerFaceAlpha', 0.5); % Adjust alpha as   needed
hold off; % Release hold on current plot
% Additional formatting (optional)
xlabel('X-axis');
ylabel('Y-axis');
title('Error Bar Plot with Transparent Markers');
legend('Error Bars', 'Data Points');

Please see attached.

For more information on MarkerFaceAlpha defined in scatter function, please refer to

https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.scatter-properties.html

So, in the above code, sample data for x, y and error values (err) is defined. The errorbar function is called with k- which specifies a black dash-dot line for error bars. The scatter function is used to overlay filled circles (k for black) at each data point with a specified size (100 points) and a transparency level (MarkerFaceAlpha', 0.5). You can adjust this alpha value between 0 (fully transparent) and 1 (fully opaque) according to your preference.

Using this method allows you to have complete control over both the appearance of your error bars and the transparency of your markers. The combination of these two functions gives you more flexibility than using errorbar alone. If you frequently need to adjust marker properties such as transparency or color dynamically based on data conditions, consider creating a custom plotting function or script that incorporates this approach for efficiency.

Hope this helps.

If you have any further questions or need additional assistance, feel free to ask!

  4 个评论
dpb
dpb 2024-10-20
@Umar "... we are always looking to improve our features..."
"we"??? Are you a Mathworks employee, now?

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2024-10-19
"... 'errorbar' does not support ... 'MarkerFaceAlpha' ..."
That would be a good enhancement request; seems no real reason is shouldn't other than the developer didn't choose to include.
The workaround would be to plot the errorbar w/o the marker and then use scatter to draw the markers which does support 'MarkerFaceAlpha'
You can probably also get the effect you want/need by simply multiplying the 'MarkerFaceColor' rgb triplet by a factor <1.
  2 个评论
Bo
Bo 2024-10-20
编辑:Bo 2024-10-20
Hi @dpb
Thanks for your suggestion! and also the check that FaceAlpha is indeed not supported by 'errorbar' function. Will use scatter + errorbar now.
Fyi: I marked @Umar's answer as 'Accepted', in which I also referred to your answer in the reply.
I also just submitted an Enhancement Suggestion.
Best,
Bo

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by