How do I align gridlines for two y-axis scales?

I am not sure if this is the prettiest way to do it, but it does fix it with one line: import matplotlib.pyplot as plt import seaborn as sns import numpy as np import pandas as pd np.random.seed(0) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(pd.Series(np.random.uniform(0, 1, size=10))) ax2 = ax1.twinx() ax2.plot(pd.Series(np.random.uniform(10, 20, size=10)), color=”r”) # … Read more

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 = [ for i in unique] … Read more

Line plot with arrows in matplotlib

You can do this with quiver, but it’s a little tricky to get the keyword arguments right. import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 10) y = np.sin(x) plt.figure() plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], scale_units=”xy”, angles=”xy”, scale=1)

making matplotlib scatter plots from dataframes in Python’s pandas

Try passing columns of the DataFrame directly to matplotlib, as in the examples below, instead of extracting them as numpy arrays. df = pd.DataFrame(np.random.randn(10,2), columns=[‘col1′,’col2’]) df[‘col3’] = np.arange(len(df))**2 * 100 + 100 In [5]: df Out[5]: col1 col2 col3 0 -1.000075 -0.759910 100 1 0.510382 0.972615 200 2 1.872067 -0.731010 500 3 0.131612 1.075142 1000 … Read more