Issue
I have plotted points from 4 arrays in the following manner. I have plotted them in the same figure by calling plt.plot twice.
import matplotlib.pyplot as plt
plt.plot(ar1,ar2,'b^',label='classical')
plt.plot(ar3,ar4,'go',label='customized')
Now I want to plot the points with a color scale according to a variable sum1 and sum2. sum1 is a reference for arr1 and arr2. sum2 is a reference for arr3 and arr4. How big is the value of sum1(for arr1 and arr2) or sum2(for arr3 and arr4) is going to decide the color of the points that are plotted with the arrays. Preferrable on a color scale from red to green, the points have to be plotted. Now I have plotted the values only with 2 colors (blue and green) as written in the code above. Now I have to plot them referring to the sum values and on a color scale from red to green. How should I proceed?
Solution
So I am not sure I fully grasped your question but here is an attempt - hope you find it useful!
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
# Make some fake data
# Fixing random state for reproducibility
np.random.seed(19680801)
# Random data arrays
ar1 = np.random.randn(100)
ar2 = np.random.randn(100)
ar3 = np.random.randn(100)
ar4 = np.random.randn(100)
t = np.arange(100)
# Variables Sum1 and Sum2
sum1 = np.sum(np.sum(ar1) + np.sum(ar2))
sum2 = np.sum(np.sum(ar3) + np.sum(ar4))
# Colormap
cmap = plt.get_cmap('RdYlGn')
min_cmap = sum1 # Bottom of colorbar is variable sum1
max_cmap = sum2 # Top of colorbar is variable sum2
# Start figure
fig, ax = plt.subplots()
fig.suptitle("A Title")
# Set ar1/ar2 and ar3/ar4 in same axis with same colormap
ax.scatter(ar1, ar2, c=t, marker='v', vmin=min_cmap, vmax=max_cmap, cmap=cmap, label='classical')
ax.scatter(ar3, ar4, c=t, marker='o', vmin=min_cmap, vmax=max_cmap, cmap=cmap, label='customized')
ax.set_ylabel("Y label")
ax.set_xlabel("X label")
ax.legend()
fig.colorbar(mappable=plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(min_cmap, max_cmap)), ax=ax,
orientation='vertical', format='%.1f', label="Sum1 (ar1 + ar2) to Sum2 (ar3 + ar4)")
plt.tight_layout()
plt.show()
Answered By - just_another_profile
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.