But that is ugly IMO to loop through a DataFrame row by row and I strongly believe there is a better way. That would result in 4 plt.scatter(.) as there are 4 groups in total. Then plot them with the marker argument set with the list defined (like plt.scatter(., marker=markers). Draw two plots on the same figure: import matplotlib.pyplot as plt. I can for loop over the DataFrame and group the entries by their group. TypeError: 'Series' objects are mutable, thus they cannot be hashed I thought plt.scatter(x=df, y=df, c=df, marker=df) cmap str or Colormap, default: rcParams'image. See matplotlib.markers for more information about marker styles. marker can be either an instance of the class or the text shorthand for a particular marker. For example the red entries will have marker "o" (big dot), green entries with marker "^" (upward triangle) and so on. marker MarkerStyle, default: rcParams'scatter.marker' (default: 'o') The marker style. scatter () s scatter : (x, y, sNone, c'b', marker'o', cmapNone, normNone, vminNone, vmaxNone, alphaNone, linewidthsNone, facetedTrue, vertsNone, holdNone, kwargs) s markersize x y. I want the groups to have different marker styles as well. # plt.scatter(x=df, y=df, c=df, marker=df) Minimal working code import matplotlib.pyplot as pltĭf = np.random.randint(100, size=100)ĭf = np.random.randint(4, size=100)ĭf = df.apply(lambda x: colors], axis=1)ĭf = df.apply(lambda x: markers], axis=1) They are grouped by the column group and I want them to have different marker styles based on the group. import matplotlib.pyplot as pltįor different shapes, override the ``get_patch`` method, and add the newĪx_r.legend(ax_r_handles, ax_r_labels, handlelength=CONF.I am trying to plot a scatter graph on some data with grouping. Furthermore, I've refactored and trimmed the code a bit to make it more modular. If more than two colours is desired, I cannot think of any other way to do this other than the approach described by ( Python Matplotlib Multi-color Legend Entry) M4, = ax.plot(,, c='magenta', marker='s', markersize=20,Īx.legend(((m2, m1), (m3, m4)), ('Foo', 'Foo2'), numpoints=1, labelspacing=2,ĭisclaimer: This will only work for a two-colors legend entry. import matplotlib.pyplot as pltĪx = fig.add_axes() Note that the numpoints=1 argument in plt.legend is important in order to display only one marker for each entry. But that is ugly IMO to loop through a DataFrame row by row and I strongly believe. That would result in 4 plt.scatter (.) as there are 4 groups in total. Then plot them with the marker argument set with the list defined (like plt.scatter (., markermarkers group). The code below show how this can be done. I can for loop over the DataFrame and group the entries by their group. Two different colours can then be attributed to each marker in order to produce the desired two-colour legend entry. The strategy is then to set the fillstyle of the first square marker to left while the other one is set to right (see ). The solution I am proposing is to combine two different proxy-artists for one entry legend, as described here: Combine two Pyplot patches for legend.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |