For a little jupyter notebook widget i tried the following:
with output:
fig, ax = plt.subplots(constrained_layout=True, figsize=(6, 4))
# move the toolbar to the bottom
fig.canvas.toolbar_position = 'bottom'
ax.grid(True)
line, = ax.plot(df.index, init_data, initial_color,)
ax.set_ylabel('CH4_HYD1')
ax.set_ylabel('')
But the data gets plotted in a wierd way. When isolating the plotting method and comparing it to the pandas default, the pandas plot displays the data correctly.
df['CH4_HYD1'].plot()
Pandas plot
plt.plot(['CH4_HYD1'])
Wrong plot?
I would like to use the axis plot method to produce a plot with multiple lines of different features. But I can't find the probably very simple error... What is the difference here?
This is a short example of how to plot multiple lines from a DataFrame with matplotlib and with pandas. Essentially, pandas uses matplotlib to do the plots so it shouldn't be different.
Anyway, I encourage you to look into seaborn. This is a great library based on pandas and matplotlib that enables visualizing dataframes easily.
Related
I have a complete matplotlib figure with multiple subplots and I want to add a vertical span in a specific range to all of the subplots. The remaining plot is then converted to plotly.
If I try running xvspan over each individual subplot when creating the matplotlib figure, the vertical spans do not make it into the plotly figure.
If I try converting the figure into plotly first and then adding a vrect, this vrect only seems to work for the first subplot. I have tried using row = "all" to no avail.
Any other ideas?
Thanks!
import pandas as pd
import seaborn as sns
# load data
df = sns.load_dataset('penguins', cache=False)
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.show()
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.show()
When I draw two plots with seaborn, in one cell, in jupyter, I get this view:
I want to draw the plots, side by side, like this:
plot1 plot2
How I should do this?
Updated:
Not two plots on one figure, but two plots on two separate figures.
This is not the solution being sought, because it's two plots on one figure.
fig, ax = plt.subplots(1,2)
sns.plotType(someData, ax=ax[0]) # plot1
sns.plotType(someData, ax=ax[1]) # plot2
fig.show()
The solutions from the proposed duplicate ipython notebook arrange plots horizontally, do not work
The option with %html causes the figures to plot on top of each other
Additionally, other options were for ipython, not Jupyter, or recommended creating subplots..
This is probably the simplest solution. Other solutions would likely involve hacking the backend environment of Jupyter.
This question is about displaying two figures, side by side.
Two separate figures, side by side, executed from a code cell, does not work.
You will need to create the separate figures, and the use plt.savefig('file.jpg') to save each figure to a file.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# load data
df = sns.load_dataset('penguins', cache=False)
# create and save figure
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.savefig('bill.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
# create and save new figure
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.savefig('flipper.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
Once the figures are saved to a file, they can be displayed side by side, by loading them in a markdown cell.
If the images are to large, the second figure will go to a new line.
Then execute the cell
I have a list of photometric redshifts and spectroscopic redshifts, and I need to make a scatterplot of these numbers to compare them. The problem is that I don't know how to make a scatterplot in python. How do you graph a scatterplot in python?
Simple Approach
First import the matplotlib package
Use the plot method, then the scatter method (both contained within the matplotlib package) to create the scatterplot
import matplotlib
%matplotlib inline # to ensure the scatter output will be shown instead of code
your_data = pd.read_csv('your_dataset')
data = your_data # to avoid typing your_data each time
scatterplot = data.plot.scatter(x='select_your_x_axis', y='select_your_y_axis')
scatterplot.plot()
Hope this helps :)
I have some python code that plotted a scatter graph with marginal histograms. This worked fine with seaborn 0.5 and still does work if I go back to this version. However, I'd like to get it to work with 0.7!
I don't use python often and mainly only use scripts set up by others that I adjust slightly.
The problem appears to be happening in the following line:
sns.lmplot(x='Moisture Content (%)', y='Dry Density (kg/m3)', hue='Test',
data=data, ax=ax_joint, fit_reg=False, legend=False, palette=test_colours)
ax is no longer recognised in seaborn 0.7.
TypeError: lmplot() got an unexpected keyword argument 'ax'
The output still gives me completed marginal histograms but the scatter graph is blank.
If you need more info then please let me know.
Cheers
seaborn lmplot ax will show you that lmplot returns you a figure-level instead of axes-level. So in order to manage lmplot use FacetGrid.
FacetGrid will plot on different variables that you states in different graphs.
import matplotlib.pyplot as plt
g = sns.FacetGrid(tips, col="time", row="smoker")
g = g.map(plt.hist, "total_bill")
FacetGrid result image
You can imagine doing a groupby on the dataframe which gives you the different graphs.
Refer to this for more example on plotting lmplot
In general, I don't have any problem to put two plots in a figure like plot(a);plot(b) in matplotlib. Now I am using a particular library which would generate a figure and I want to overlay with boxplot. Both are generated by matplotlib. So I think it should be fine but I can only see one plot. Here is the code. I am using beeswarm and here is its ipython notebook. I can only plot beeswarm or boxplot but not both in a figure. My main goal is trying to save column scatter plot and boxplot together as a figure in pdf. Thanks,
from beeswarm import beeswarm
fig=plt.figure()
figure(figsize=(5,7))
ax1=plt.subplot(111)
fig.ylim=(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2)
The problem is with the positioning of the box plot. The default positioning list starts with 1 what shifts the plot to 1 and your beeswarm plot sits on 0.
So the plots are in different places of your canvas.
I modified your code a little bit and that seems to solve your problem.
from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])
Cheers