I want to change y axis ticks in python. I am using the code
import pylab as plt
y1 = [0,1,2,...10]
y2 = [90,40,65,12,....]
labels = [0.30,0.29,0.28,....]
plt.plot(y1)
plt.plot(y2,'r')
plt.yticks(y1, labels)
plt.yticks(y2, labels)
plt.show()
But all the y axis label appear at one place on top of one another
Borrowing heavily from this example, the code below demonstrates one possible way to have two plots on one figure.
import pylab as plt
fig, ax1 = plt.subplots()
y1 = [0,1,2,3,4,5,6,7,8,9,10]
labels = [0.30,0.29,0.28,0.27,0.26,0.25,0.24,0.23,0.22,0.21,0.20]
ax1.plot(labels, y1, 'b-')
ax1.set_xlabel('labels')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('y1', color='b', rotation="horizontal")
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
y2 = [90,40,65,12,23,43,54,13,42,53,63]
ax2.plot(labels, y2, 'r-')
ax2.set_ylabel('y2', color='r', rotation="horizontal")
ax2.tick_params('y', colors='r')
fig.tight_layout()
plt.show()
Related
I am trying to make a visualization with logarithmic ticks on all sides of the box.
import numpy as np
import matplotlib.pyplot as plt
x = np.logspace(2, 5, 5)
y = 0.5*x**(-1/2)
y2 = 0.01*x**(-1/2)
y3 = 0.05*x**(-1/3)
fig, ax = plt.subplots()
ax.plot(x, y, 'o-', label="One")
ax.plot(x, y2, '*-', label="Two")
ax.plot(x, y3, '--', label="Three")
ax.set(
xlabel='Input',
xlim=(1e2, 1e5),
xscale='log',
ylabel='Output',
ylim=(1e-5, 1e-1),
yscale='log',
)
ax.tick_params(top=True, right=True) # <-- This didn't work how I expected.
ax.legend(loc='lower left');
I would like the associated minor tick marks on the top and right spine.
Any advice on how to make that happen?
Use the which parameter of Axes.tick_params:
ax.tick_params(which='both', top=True, right=True)
Output:
i wanted to know how to make a plot with two y-axis so that my plot that looks like this :
to something more like this by adding another y-axis :
i'm only using this line of code from my plot in order to get the top 10 EngineVersions from my data frame :
sns.countplot(x='EngineVersion', data=train, order=train.EngineVersion.value_counts().iloc[:10].index);
I think you are looking for something like:
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [1000,2000,500,8000,3000]
y1 = [1050,3000,2000,4000,6000]
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.bar(x, y)
ax2.plot(x, y1, 'o-', color="red" )
ax1.set_xlabel('X data')
ax1.set_ylabel('Counts', color='g')
ax2.set_ylabel('Detection Rates', color='b')
plt.show()
Output:
#gdubs If you want to do this with Seaborn's library, this code set up worked for me. Instead of setting the ax assignment "outside" of the plot function in matplotlib, you do it "inside" of the plot function in Seaborn, where ax is the variable that stores the plot.
import seaborn as sns # Calls in seaborn
# These lines generate the data to be plotted
x = [1,2,3,4,5]
y = [1000,2000,500,8000,3000]
y1 = [1050,3000,2000,4000,6000]
fig, ax1 = plt.subplots() # initializes figure and plots
ax2 = ax1.twinx() # applies twinx to ax2, which is the second y axis.
sns.barplot(x = x, y = y, ax = ax1, color = 'blue') # plots the first set of data, and sets it to ax1.
sns.lineplot(x = x, y = y1, marker = 'o', color = 'red', ax = ax2) # plots the second set, and sets to ax2.
# these lines add the annotations for the plot.
ax1.set_xlabel('X data')
ax1.set_ylabel('Counts', color='g')
ax2.set_ylabel('Detection Rates', color='b')
plt.show(); # shows the plot.
Output:
Seaborn output example
You could try this code to obtain a very similar image to what you originally wanted.
import seaborn as sb
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
x = ['1.1','1.2','1.2.1','2.0','2.1(beta)']
y = [1000,2000,500,8000,3000]
y1 = [3,4,1,8,5]
g = sb.barplot(x=x, y=y, color='blue')
g2 = sb.lineplot(x=range(len(x)), y=y1, color='orange', marker='o', ax=g.axes.twinx())
g.set_xticklabels(g.get_xticklabels(), rotation=-30)
g.set_xlabel('EngineVersion')
g.set_ylabel('Counts')
g2.set_ylabel('Detections rate')
g.legend(handles=[Rectangle((0,0), 0, 0, color='blue', label='Nontouch device counts'), Line2D([], [], marker='o', color='orange', label='Detections rate for nontouch devices')], loc=(1.1,0.8))
In pyplot, you can change the order of different graphs using the zorder option or by changing the order of the plot() commands. However, when you add an alternative axis via ax2 = twinx(), the new axis will always overlay the old axis (as described in the documentation).
Is it possible to change the order of the axis to move the alternative (twinned) y-axis to background?
In the example below, I would like to display the blue line on top of the histogram:
import numpy as np
import matplotlib.pyplot as plt
import random
# Data
x = np.arange(-3.0, 3.01, 0.1)
y = np.power(x,2)
y2 = 1/np.sqrt(2*np.pi) * np.exp(-y/2)
data = [random.gauss(0.0, 1.0) for i in range(1000)]
# Plot figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax2.hist(data, bins=40, normed=True, color='g',zorder=0)
ax2.plot(x, y2, color='r', linewidth=2, zorder=2)
ax1.plot(x, y, color='b', linewidth=2, zorder=5)
ax1.set_ylabel("Parabola")
ax2.set_ylabel("Normal distribution")
ax1.yaxis.label.set_color('b')
ax2.yaxis.label.set_color('r')
plt.show()
Edit: For some reason, I am unable to upload the image generated by this code. I will try again later.
You can set the zorder of an axes, ax.set_zorder(). One would then need to remove the background of that axes, such that the axes below is still visible.
ax2 = ax1.twinx()
ax1.set_zorder(10)
ax1.patch.set_visible(False)
This question already has answers here:
increase the linewidth of the legend lines in matplotlib
(4 answers)
Closed 5 years ago.
I would like to change the thickness/width of the line samples featured in the pyplot legend.
Line width of line samples within legend are the same as the lines they represent in the plot (so if line y1 has linewidth=7.0, the legend's corresponding y1 label will also have linewidth=7.0).
I would like the legend lines to be thicker than lines featured in the plot.
For example, the following code generates the following image:
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
# plot sin(x) and cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1',linewidth=7.0)
ax.plot(x, y2, c='r', label='y2')
leg = plt.legend()
plt.show()
I want to set the y1 label in the legend to have linewidth=7.0, while the y1 line featured in the plot has a different width (linewidth=1.0).
Related issues had answers for changing the linewidth of the legend bounding box through leg.get_frame().set_linewidth(7.0). This does not change linewidth of the lines within the legend.
#ImportanceOfBeingErnest 's answer is good if you only want to change the linewidth inside the legend box. But I think it is a bit more complex since you have to copy the handles before changing legend linewidth. Besides, it can not change the legend label fontsize. The following two methods can not only change the linewidth but also the legend label text font size in a more concise way.
Method 1
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
# plot sin(x) and cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1')
ax.plot(x, y2, c='r', label='y2')
leg = plt.legend()
# get the individual lines inside legend and set line width
for line in leg.get_lines():
line.set_linewidth(4)
# get label texts inside legend and set font size
for text in leg.get_texts():
text.set_fontsize('x-large')
plt.savefig('leg_example')
plt.show()
Method 2
import numpy as np
import matplotlib.pyplot as plt
# make some data
x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
# plot sin(x) and cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y1, c='b', label='y1')
ax.plot(x, y2, c='r', label='y2')
leg = plt.legend()
# get the lines and texts inside legend box
leg_lines = leg.get_lines()
leg_texts = leg.get_texts()
# bulk-set the properties of all lines and texts
plt.setp(leg_lines, linewidth=4)
plt.setp(leg_texts, fontsize='x-large')
plt.savefig('leg_example')
plt.show()
The above two methods produce the same output image:
I'm writing a program, which has two outputs: a GUI and a printed report on paper (a simple pdf printed out).
On both outputs I would like to have a diagram, but with different styles.
dark_background on the GUI (http://matplotlib.org/examples/style_sheets/plot_dark_background.html)
and fivethirtyeight on the paper http://matplotlib.org/examples/style_sheets/plot_fivethirtyeight.html
Somehow I could not manage to generate 2 images with proper styles. Only one of them was correct. I do not have enough experience-points to post pictures. So I will post only my code.
My first idea was:
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plt.savefig('result_dark.png')
plt.style.use('fivethirtyeight')
plt.savefig('result_white.png')
But it did not work properly. One of the images was correct. The second had a correct backgroundcolor, but the fontcolor of legend/labels did not change. I tried to separate the 2 images, the result was the same:
import numpy as np
import matplotlib.pyplot as plt
import os
def plot():
#set pic size
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [1,2,3,1,2,3,1,2,3,1]
y3 = [3,1,2,3,1,2,3,1,2,3]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.2, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,5.3))
# Labels
ax.set_xlabel('Messpunkte-Nr.\nMeasurement points number')
ax.set_ylabel('Spezifikationsgrenze normiert\nnormed to specification')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
texts =legend.get_texts()
texts[0].set_color('green')
texts[1].set_color('orange')
texts[2].set_color('purple')
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
#plt.close()
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
plt.clf()
#plt.close()
How should I fix my code to have 2 images with the same values, but different styles?
I would suggest structuring you code something like:
from matplotlib.style import context
def my_plot_function(ax, data, style):
# do all of your plotting in here, should be fast as no computation
pass
with context('dark'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
with context('fivethirtyeight'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
This is a design feature, not a bug. Almost all of the values controlled by rcparams are set at object creation time, not a draw time, because having what your figure will look like when you render it depend on global state is terrifying. This also allows you to use context managers for the rcparams, as shown above. Calling use only over-rides the values that the style sheet explicitly sets (which is also a design feature so you can apply multiple styles a-la cascading style sheets).
So your problem appears to be that you do a lot of plotting and then tell pylab you'd like your plots to have a particular style. That instruction doesn't seem to be updating everything. So instead, tell it you want to use a particular style. Then plot. Then clear everything. Then plot again.
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
Does this give what you want? Here are the figures I got.