Import matplotlib.pyplot as plt x = y = size = plt.scatter(x, y, s=size, c='coral', label='class 1') x = y = size = plt.scatter(x, y, s=size, c='lightblue', label='class 2') plt.legend() plt.title('Nuage de points avec Matplotlib') plt.xlabel('x') plt.ylabel('y') plt.savefig('ScatterPlot_04.png') plt. Import matplotlib.pyplot as plt x = y = size = plt.scatter(x, y, s=size, c='coral') x = y = size = plt.scatter(x, y, s=size, c='lightblue') plt.title('Nuage de points avec Matplotlib') plt.xlabel('x') plt.ylabel('y') plt.savefig('ScatterPlot_03.png') plt.show() Add a legendįinally, it also possible to add a legend: To change the color there is the argument c in the function scatter(), example: Note: to change the size of all the points, just do s = 300 for example. Import matplotlib.pyplot as plt x = y = size = plt.scatter(x,y,s=size) plt.title('Nuage de points avec Matplotlib') plt.xlabel('x') plt.ylabel('y') plt.savefig('ScatterPlot_02.png') plt.show() The following also demonstrates how transparency of the markers can be adjusted by giving alpha a value between 0 and 1. y plt.scatter(x, y, label'Original Data', color'steelblue') add legend plt.legend() display plot plt. To create a scatter plot with a legend one may use a loop and create one scatter plot per item to appear in the legend and set the label accordingly. It is possible to increase the point size by specifying the argument s (size) in the function scatter(): The following code shows how to create a scatter plot in matplotlib with a default legend: import matplotlib.pyplot as plt define data to plot x 1, 2, 3, 4, 5, 6, 7 y 2, 3, 5, 8, 12, 18, 27 create scatter plot of x vs. Unlike matplotlib, proplot adds outer colorbars and legends by allocating new rows and columns in the GridSpec rather than stealing space from the parent. handles scatter. Import matplotlib.pyplot as plt x = y = plt.scatter(x,y) plt.title('Nuage de points avec Matplotlib') plt.xlabel('x') plt.ylabel('y') plt.savefig('ScatterPlot_01.png') plt.show() Increase the point size .legendelements returns legend handles and labels for a PathCollection. How to create a simple scatter plot using matplotlib ? To plot a scatter plot with matplotlib, ta solution is to use the method scatter from the class pyplot, example:
0 Comments
Leave a Reply. |