Adding a legend to PyPlot in Matplotlib in the simplest manner possible

Add a label= to each of your plot() calls, and then call legend(loc=”upper left”). Consider this sample (tested with Python 3.8.0): import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 20, 1000) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, “-b”, label=”sine”) plt.plot(x, y2, “-r”, label=”cosine”) plt.legend(loc=”upper left”) plt.ylim(-1.5, 2.0) plt.show() Slightly modified … Read more

Remove xticks in a matplotlib plot?

The plt.tick_params method is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis. Note that there is also ax.tick_params for matplotlib.axes.Axes objects. from matplotlib import pyplot as plt plt.plot(range(10)) plt.tick_params( axis=”x”, # changes apply to the x-axis which=”both”, # both major and minor … Read more

How to change legend title in ggplot

This should work: p <- ggplot(df, aes(x=rating, fill=cond)) + geom_density(alpha=.3) + xlab(“NEW RATING TITLE”) + ylab(“NEW DENSITY TITLE”) p <- p + guides(fill=guide_legend(title=”New Legend Title”)) (or alternatively) p + scale_fill_discrete(name = “New Legend Title”)

Hiding axis text in matplotlib plots

Instead of hiding each element, you can hide the whole axis: frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) Or, you can set the ticks to an empty list: frame1.axes.get_xaxis().set_ticks([]) frame1.axes.get_yaxis().set_ticks([]) In this second option, you can still use plt.xlabel() and plt.ylabel() to add labels to the axes.

When to use cla(), clf() or close() for clearing a plot in matplotlib?

They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will discuss both cases below. pyplot interface pyplot is a module that collects … Read more

Changing the tick frequency on the x or y axis

You could explicitly set where you want to tick marks with plt.xticks: plt.xticks(np.arange(min(x), max(x)+1, 1.0)) For example, import numpy as np import matplotlib.pyplot as plt x = [0,5,9,10,15] y = [0,1,2,3,4] plt.plot(x,y) plt.xticks(np.arange(min(x), max(x)+1, 1.0)) plt.show() (np.arange was used rather than Python’s range function just in case min(x) and max(x) are floats instead of ints.) … Read more

Save plot to image file instead of displaying it using Matplotlib

When using matplotlib.pyplot.savefig, the file format can be specified by the extension: from matplotlib import pyplot as plt plt.savefig(‘foo.png’) plt.savefig(‘foo.pdf’) That gives a rasterized or vectorized output respectively. In addition, there is sometimes undesirable whitespace around the image, which can be removed with: plt.savefig(‘foo.png’, bbox_inches=”tight”) Note that if showing the plot, plt.show() should follow plt.savefig(); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)