## How to create a scatter plot by category [duplicate]

You can use scatter for this, but that requires having numerical values for your key1, and you won’t have a legend, as you noticed. It’s better to just use plot for discrete categories like this. For example: import matplotlib.pyplot as plt import numpy as np import pandas as pd np.random.seed(1974) # Generate Data num = … Read more

## How to add trendline to a scatter plot

as explained here With help from numpy one can calculate for example a linear fitting. # plot the data itself pylab.plot(x,y,’o’) # calc the trendline z = numpy.polyfit(x, y, 1) p = numpy.poly1d(z) pylab.plot(x,p(x),”r–“) # the line equation: print “y=%.6fx+(%.6f)”%(z[0],z[1])

## How to plot a scatter plot with a legend label for each class

Actually both linked questions provide a way how to achieve the desired result. The easiest method is to create as many scatter plots as unique classes exist and give each a single color and legend entry. import matplotlib.pyplot as plt x=[1,2,3,4] y=[5,6,7,8] classes = [2,4,4,2] unique = list(set(classes)) colors = [plt.cm.jet(float(i)/max(unique)) for i in unique] … Read more

## Color a scatter plot by Column Values

Imports and Data import numpy import pandas import matplotlib.pyplot as plt import seaborn as sns seaborn.set(style=”ticks”) numpy.random.seed(0) N = 37 _genders= [‘Female’, ‘Male’, ‘Non-binary’, ‘No Response’] df = pandas.DataFrame({ ‘Height (cm)’: numpy.random.uniform(low=130, high=200, size=N), ‘Weight (kg)’: numpy.random.uniform(low=30, high=100, size=N), ‘Gender’: numpy.random.choice(_genders, size=N) }) Update August 2021 With seaborn 0.11.0, it’s recommended to use new figure … Read more

## Setting different color for each series in scatter plot

I don’t know what you mean by ‘manually’. You can choose a colourmap and make a colour array easily enough: import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm x = np.arange(10) ys = [i+x+(i*x)**2 for i in range(10)] colors = cm.rainbow(np.linspace(0, 1, len(ys))) for y, c in zip(ys, colors): plt.scatter(x, y, … Read more

## plot different color for different categorical levels

Imports and Sample DataFrame import matplotlib.pyplot as plt import pandas as pd import seaborn as sns # for sample data from matplotlib.lines import Line2D # for legend handle # DataFrame used for all options df = sns.load_dataset(‘diamonds’) carat cut color clarity depth table price x y z 0 0.23 Ideal E SI2 61.5 55.0 326 … Read more

## How to color scatter markers as a function of a third variable

There’s no need to manually set the colors. Instead, specify a grayscale colormap… import numpy as np import matplotlib.pyplot as plt # Generate data… x = np.random.random(10) y = np.random.random(10) # Plot… plt.scatter(x, y, c=y, s=500) # s is a size of marker plt.gray() plt.show() Or, if you’d prefer a wider range of colormaps, you … Read more

## scatter plot in jfreechart from database

This complete example creates a suitable database table in memory, queries it into a JDBCXYDataset and displays the dataset in a scatter plot. Note how the first column becomes the domain, while successive columns become individual series. import java.awt.EventQueue; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.Calendar; import java.util.Random; … Read more

## Adding legend based on existing color series

You can create the legend handles using an empty plot with the color based on the colormap and normalization of the scatter plot. import pandas as pd import numpy as np; np.random.seed(1) import matplotlib.pyplot as plt x = [np.random.normal(5,2, size=20), np.random.normal(10,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,1, size=20)] y = [np.random.normal(5,1, size=20), np.random.normal(5,1, size=20), np.random.normal(10,2, size=20), np.random.normal(10,2, … Read more

## Get data from plot with matplotlib

Using a slightly modified version of Joe Kington’s DataCursor: import matplotlib.pyplot as plt import matplotlib.mlab as mlab import matplotlib.cbook as cbook import numpy as np def fmt(x, y): return ‘x: {x:0.2f}\ny: {y:0.2f}’.format(x = x, y = y) class DataCursor(object): # https://stackoverflow.com/a/4674445/190597 “””A simple data cursor widget that displays the x,y location of a matplotlib artist … Read more