-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Description
Bug summary
The color of the legend is correct, but why do I have two colors for the color of the image above, the gray at the top of the image I did not define. The other image has exactly the right color.
What is causing this and can I circumvent it?
Code for reproduction
import matplotlib as mpl # noqa: F401
import matplotlib.pyplot as plt
import numpy as np
import scienceplots # noqa: F401
from matplotlib.patches import Rectangle
from matplotlib.ticker import MultipleLocator, ScalarFormatter
from mpl_toolkits.mplot3d import Axes3D
# initial settings
# mpl.rcParams["svg.fonttype"] = "none"
plt.style.use(["science", "grid", "no-latex"])
# Grid data X, Y and meshgrid. X -> \rho, Y -> c2
X, Y = np.meshgrid(np.linspace(51 / 98, 1, 15), np.linspace(1933 / 17, 275, 15))
# Set parameter values
U, MU = 20000, 10000
H0, H1, HG = 10, 5, 12
C0, C1 = 25, 30
G0, G = 20, 25
V, S = 8, 5
W = 150
M = 400
O = 10 # noqa: E741
E = 300
# Calculate the values for Qg, qm, qs, Qa, and qa
Qg = U * (1 - (W + HG - V) / (X * (E - V)))
qm = U * (1 - (C1 + H0 + G0 - V - O) / (X * (Y + G - V))) - Qg
qs = U * (1 - (C0 + H1 - S) / (X * (Y - S))) - qm - Qg
Qa = U * (1 - (W + HG - V) / (X * (E - V)))
qa = U * (1 - (C1 + G0 + H0 - V) / (X * (E - V))) - Qa
# 给出高度Z的值
benchmark_profit_s = (Qa + qa) * (C1 - C0)
mainmodel_profit_s = (
(C1 - C0) * Qg
+ (C1 - C0) * qm
+ (S + O - C0 - H1) * qs
+ X * (Y - S) * (qs - ((Qg + qm + qs) ** 2 - (Qg + qm) ** 2) / (2 * U))
)
# Create the figure and axes objects
fig = plt.figure(figsize=(7, 5.5))
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)
# Plot the surfaces
ax.plot_surface(X, Y, benchmark_profit_s, label="Benchmark", color="#111111")
ax.plot_surface(X, Y, mainmodel_profit_s, label="MainModel", color="#888888")
ax.invert_xaxis()
ax.set_xlabel("$\\rho$", size=22, labelpad=10)
ax.set_ylabel("$c_2$", size=22, labelpad=10)
ax.tick_params(labelsize=18)
ax.xaxis.set_minor_locator(MultipleLocator(0.1))
ax.yaxis.set_major_locator(MultipleLocator(30))
ax.yaxis.set_minor_locator(MultipleLocator(30))
ax.zaxis.get_offset_text().set(size=18)
z_formatter = ScalarFormatter(useMathText=True)
z_formatter.set_scientific(True)
z_formatter.set_powerlimits((-2, 2))
z_axis = ax.get_zaxis()
z_axis.set_major_formatter(z_formatter)
# show legend
colors = ["#111111", "#888888"]
legend_labels = ["$E\,\\Pi_{as}$", "$E\,\\Pi_s$"]
legend_handles = [Rectangle((0, 0), 1, 1, fc=color) for color in colors]
ax.legend(
handles=legend_handles,
labels=legend_labels,
fontsize=20,
frameon=True,
fancybox=False,
shadow=True,
edgecolor="black",
loc="lower right",
bbox_to_anchor=(1.15, 0.78),
)
# Set the view angle and display the plot
ax.view_init(elev=28, azim=-45)
plt.show()Actual outcome
Expected outcome
Like the other picture, the colors are displayed correctly. Like this.
Additional information
No response
Operating system
No response
Matplotlib Version
3.8.3
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
pip

