How to plot two different graphs on a single figure in pandas? - python

I am trying to get two different plots as one plot. I will not write down my entire code (is so long), but based on the two small codes below, i get two different time series and I want to put these together in one figure.
My code for the first plot:
plt.figure(figsize=(15,4))
i = plt.plot(july/july.mean(),label='G')
my code for my second plot:
spi3 = pd.read_csv('SPI3.csv',header=0,parse_dates=True)
spi3.plot(y='spi',figsize=(16,4))

Quick dirty fix would be to plot dictionaries at first, only then plot with plt.plot. Also, if you want to plot in the same figure, define figsize only in the first figure you are plotting. (Therefore plt.figure is ommitted completely.)
spi3.plot(y='spi',figsize=(16,4))
plt.plot(july/july.mean(),label='G')

Related

How do I share labels across collections in matplotlib? Or how do I produce the legend cleanly?

I am trying to use matplotlib to show some data in a clear way. My current goal is to label the data using two methods: color and shape. The color will be used to represent the data set these specific points come from, while the shape is used to represent whether that example is in category one or two. To visualize this, here is a simple example I drew in PowerPoint:
The reason for doing this instead of simply creating a legend with each specific data set and category stated is I am plotting upwards of 10 data sets, so the legend would remain significantly cleaner and easier to read if color was used for the data sets and shape used for general category (thus the legend would show 10 colors and two shapes, as opposed to 20 different color-shape combinations).
I am currently able to use matplotlib to set the label of the individual data sets by iterating through them and plotting each individually as follows:
import matplotlib.pyplot as plt
ax = plt.figure()
for data in datasets:
scat_plot = ax.scatter(data[x], data[y], label=data[label])
ax.legend()
plt.show()
However, when I attempt to plot the individual shapes and colors and assign them the same label, I am left with plots that do not recognize the two scatter collections as having the same label.
Any suggestions or hints would be greatly appreciated. Thank you.

Problem plotting three sets of data in one graph

EDIT - I was being stupid, and trying to plot strings. I converted to int and plotted again fine. Thanks to ImportanceOfBeingErnest for the hint.
I have data from 3 sensors which I want to plot, using matplotlib
Each array is of different length, and I plot them using the following line of code
plt.plot(s_1,'r',s_3,'b',s_4,'g')
plt.show()
This produces the following graph
As you can see, the green trace is not correct, and the y-axis scale is off (these is a 6 after the 21).
I'm really not sure what the problem is here.
When I plot the data individually, they are fine:
It is the last one in this series that is plotted strangely in the graph with all three at once.
To be clear, I don't understand why separately the graphs plot fine, but when the three are printed in one plot the y-axis gets messed up.
Any advice around what the issue with the three-in-one plot is would be great.

Superimposing some plots with a txt file

`I am trying to reproduce the attached figure step by step. My problem was that how can i plot colorbar in above figure by my data. My data is a cosmological data and it has 7 columns totally with many raw. My main goal is reproducing the present figure step by step. You can see that there are three different plots which are interpolated each other. Firstly, i tried to plot small colorful lines in the body of figure by using two columns of data. I did it by scatter plots and then i needed to reproduce the colorbar part of figure. But, it was not possible at the first attempt. Because, the colorbar points was not a part of data. Then, i obtained the values of colorbar by some calculations and added them as additional columns to data. Now, i could you the simple colorbar function to do colorbar part. And i got it. For the next step, i need to turn small curved lines to dark solid lines.
How can I do plots in matplotlib?
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
data1 = np.loadtxt("bei_predic.txt", unpack=True)
B = np.log10(data1[3]/(4.*(data1[2])))
R = np.vstack((data1,B))
R = np.transpose(R)
D = R[~np.isnan(R).any(axis=1)]
A = plt.scatter(D[:,3],D[:,2], c=D[:,8])
cbar= plt.colorbar()
cbar.set_label("file", labelpad=+1)
plt.show()
If you could start off by telling us a little bit about the data that you are using that would be great. In order to plot the figure that you want, we must first load the data into some variables. Have you managed to do this?
Check out this example in which the author plots multicolored lines for some guidance.

How to plot independent graphs next to each other using seaborn?

my problem is that I could only find answers for plots sharing the same y-axis units.
My graphs are defined as follows:
#Plot1
sns.set_style("white")
sns.catplot(y="Reaction_cd_positive", x="Flux_cd_positive",
kind="bar",height=4, data=CDP,aspect=1.5)
#Plot2
sns.catplot(y="Reaction_cd_negative",x="Flux_cd_negative",
kind="bar",height=4, data=CDN, aspect=1.5)
Thank you in advance!
Ok, let me translate this. You are using seaborn in a jupyter notebook. You want 2 barplots next to each other within the same figure, instead of two individual figures. Since catplot produces a figure by itself, there are two options.
Create a single catplot with two subplots. To this end you would need to concatenate your two DataFrames into a single one, then use the col argument to split the data into the two subplots.
Create a subplot grid with matplotlib first, then plot a barplot into each of the subplots. This is shown in this question.

matplotlib combine plots that are produced by other module

I would like to combine a few plots, each produced by pyblock, in one figure.
for key in reblock_data.columns.levels[0].values:
pbl.plot.plot_reblocking(reblock_data.xs(key,level=0,axis=1,drop_level=False))
plt.show()
This does not work, but rather produces a seperate figure for each key.
Maybe someone with more background could help me combine those figures.
Here is the respective doc for pyblock: pyblock.plot
The return of the method is class matplotlib.figure.Figure. Does that mean I can not combine two of them?

Categories